KiCad PCB EDA Suite
erc.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2011-2016 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #include <fctsys.h>
32 #include <class_drawpanel.h>
33 #include <kicad_string.h>
34 #include <schframe.h>
35 
36 #include <netlist.h>
37 #include <class_netlist_object.h>
38 #include <lib_pin.h>
39 #include <erc.h>
40 #include <sch_marker.h>
41 #include <sch_component.h>
42 #include <sch_sheet.h>
43 
44 #include <wx/ffile.h>
45 
46 
47 /* ERC tests :
48  * 1 - conflicts between connected pins ( example: 2 connected outputs )
49  * 2 - minimal connections requirements ( 1 input *must* be connected to an
50  * output, or a passive pin )
51  */
52 
53 
54 /*
55  * Electrical type of pins:
56  * PIN_INPUT = usual pin input: must be connected
57  * PIN_OUTPUT = usual output
58  * PIN_BIDI = input or output (like port for a microprocessor)
59  * PIN_TRISTATE = tris state bus pin
60  * PIN_PASSIVE = pin for passive components: must be connected, and can be
61  * connected to any pin
62  * PIN_UNSPECIFIED = unknown electrical properties: creates always a warning
63  * when connected
64  * PIN_POWER_IN = power input (GND, VCC for ICs). Must be connected to a power
65  * output.
66  * PIN_POWER_OUT = output of a regulator: intended to be connected to power
67  * input pins
68  * PIN_OPENCOLLECTOR = pin type open collector
69  * PIN_OPENEMITTER = pin type open emitter
70  * PIN_NC = not connected (must be left open)
71  *
72  * Minimal requirements:
73  * All pins *must* be connected (except PIN_NC).
74  * When a pin is not connected in schematic, the user must place a "non
75  * connected" symbol to this pin.
76  * This ensures a forgotten connection will be detected.
77  */
78 
79 /* Messages for conflicts :
80  * PIN_INPUT, PIN_OUTPUT, PIN_BIDI, PIN_TRISTATE, PIN_PASSIVE,
81  * PIN_UNSPECIFIED, PIN_POWER_IN, PIN_POWER_OUT, PIN_OPENCOLLECTOR,
82  * PIN_OPENEMITTER, PIN_NC
83  * These messages are used to show the ERC matrix in ERC dialog
84  */
85 
86 // Messages for matrix rows:
87 const wxString CommentERC_H[] =
88 {
89  _( "Input Pin.........." ),
90  _( "Output Pin........." ),
91  _( "Bidirectional Pin.." ),
92  _( "Tri-State Pin......" ),
93  _( "Passive Pin........" ),
94  _( "Unspecified Pin...." ),
95  _( "Power Input Pin...." ),
96  _( "Power Output Pin..." ),
97  _( "Open Collector....." ),
98  _( "Open Emitter......." ),
99  _( "No Connection......" )
100 };
101 
102 // Messages for matrix columns
103 const wxString CommentERC_V[] =
104 {
105  _( "Input Pin" ),
106  _( "Output Pin" ),
107  _( "Bidirectional Pin" ),
108  _( "Tri-State Pin" ),
109  _( "Passive Pin" ),
110  _( "Unspecified Pin" ),
111  _( "Power Input Pin" ),
112  _( "Power Output Pin" ),
113  _( "Open Collector" ),
114  _( "Open Emitter" ),
115  _( "No Connection" )
116 };
117 
118 
119 /* Look up table which gives the diag for a pair of connected pins
120  * Can be modified by ERC options.
121  * at start up: must be loaded by DefaultDiagErc
122  * Can be modified in dialog ERC
123  */
125 
134 {
135 /* I, O, Bi, 3S, Pas, UnS, PwrI, PwrO, OC, OE, NC */
136 /* I */ { OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
137 /* O */ { OK, ERR, OK, WAR, OK, WAR, OK, ERR, ERR, ERR, ERR },
138 /* Bi*/ { OK, OK, OK, OK, OK, WAR, OK, WAR, OK, WAR, ERR },
139 /* 3S*/ { OK, WAR, OK, OK, OK, WAR, WAR, ERR, WAR, WAR, ERR },
140 /*Pas*/ { OK, OK, OK, OK, OK, WAR, OK, OK, OK, OK, ERR },
141 /*UnS */ { WAR, WAR, WAR, WAR, WAR, WAR, WAR, WAR, WAR, WAR, ERR },
142 /*PwrI*/ { OK, OK, OK, WAR, OK, WAR, OK, OK, OK, OK, ERR },
143 /*PwrO*/ { OK, ERR, WAR, ERR, OK, WAR, OK, ERR, ERR, ERR, ERR },
144 /* OC */ { OK, ERR, OK, WAR, OK, WAR, OK, ERR, OK, OK, ERR },
145 /* OE */ { OK, ERR, WAR, WAR, OK, WAR, OK, ERR, OK, OK, ERR },
146 /* NC */ { ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR, ERR }
147 };
148 
149 
161 {
162 /* In Out, Bi, 3S, Pas, UnS, PwrI,PwrO,OC, OE, NC */
163 /* In*/ { NOD, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
164 /*Out*/ { DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, NPI },
165 /* Bi*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
166 /* 3S*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
167 /*Pas*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
168 /*UnS*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
169 /*PwrI*/ { NOD, DRV, NOD, NOD, NOD, NOD, NOD, DRV, NOD, NOD, NPI },
170 /*PwrO*/ { DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, DRV, NPI },
171 /* OC*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
172 /* OE*/ { DRV, DRV, DRV, DRV, DRV, DRV, NOD, DRV, DRV, DRV, NPI },
173 /* NC*/ { NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI, NPI }
174 };
175 
176 
177 int TestDuplicateSheetNames( bool aCreateMarker )
178 {
179  SCH_SCREEN* screen;
180  SCH_ITEM* item;
181  SCH_ITEM* test_item;
182  int err_count = 0;
183  SCH_SCREENS screenList; // Created the list of screen
184 
185  for( screen = screenList.GetFirst(); screen != NULL; screen = screenList.GetNext() )
186  {
187  for( item = screen->GetDrawItems(); item != NULL; item = item->Next() )
188  {
189  // search for a sheet;
190  if( item->Type() != SCH_SHEET_T )
191  continue;
192 
193  for( test_item = item->Next(); test_item != NULL; test_item = test_item->Next() )
194  {
195  if( test_item->Type() != SCH_SHEET_T )
196  continue;
197 
198  // We have found a second sheet: compare names
199  // we are using case insensitive comparison to avoid mistakes between
200  // similar names like Mysheet and mysheet
201  if( ( (SCH_SHEET*) item )->GetName().CmpNoCase(
202  ( ( SCH_SHEET* ) test_item )->GetName() ) == 0 )
203  {
204  if( aCreateMarker )
205  {
206  /* Create a new marker type ERC error*/
207  SCH_MARKER* marker = new SCH_MARKER();
208  marker->SetTimeStamp( GetNewTimeStamp() );
210  ( (SCH_SHEET*) test_item )->GetPosition(),
211  _( "Duplicate sheet name" ),
212  ( (SCH_SHEET*) test_item )->GetPosition() );
215  screen->Append( marker );
216  }
217 
218  err_count++;
219  }
220  }
221  }
222  }
223 
224  return err_count;
225 }
226 
227 
228 void Diagnose( NETLIST_OBJECT* aNetItemRef, NETLIST_OBJECT* aNetItemTst,
229  int aMinConn, int aDiag )
230 {
231  SCH_MARKER* marker = NULL;
232  SCH_SCREEN* screen;
233  ELECTRICAL_PINTYPE ii, jj;
234 
235  if( aDiag == OK )
236  return;
237 
238  /* Create new marker for ERC error. */
239  marker = new SCH_MARKER();
240  marker->SetTimeStamp( GetNewTimeStamp() );
241 
244  screen = aNetItemRef->m_SheetPath.LastScreen();
245  screen->Append( marker );
246 
247  wxString msg;
248 
249  if( aMinConn < 0 )
250  {
251  if( aNetItemRef->m_Type == NET_HIERLABEL || aNetItemRef->m_Type == NET_HIERBUSLABELMEMBER )
252  {
253  msg.Printf( _( "Hierarchical label %s is not connected to a sheet label." ),
254  GetChars( aNetItemRef->m_Label ) );
256  aNetItemRef->m_Start,
257  msg,
258  aNetItemRef->m_Start );
259  }
260  else if( aNetItemRef->m_Type == NET_GLOBLABEL )
261  {
262  msg.Printf( _( "Global label %s is not connected to any other global label." ),
263  GetChars( aNetItemRef->m_Label ) );
264  marker->SetData( ERCE_GLOBLABEL,
265  aNetItemRef->m_Start,
266  msg,
267  aNetItemRef->m_Start );
268  }
269  else
270  {
271  msg.Printf( _( "Sheet label %s is not connected to a hierarchical label." ),
272  GetChars( aNetItemRef->m_Label ) );
274  aNetItemRef->m_Start,
275  msg,
276  aNetItemRef->m_Start );
277  }
278 
279  return;
280  }
281 
282  ii = aNetItemRef->m_ElectricalPinType;
283 
284  wxString cmp_ref( "?" );
285 
286  if( aNetItemRef->m_Type == NET_PIN && aNetItemRef->m_Link )
287  cmp_ref = aNetItemRef->GetComponentParent()->GetRef( &aNetItemRef->m_SheetPath );
288 
289  if( aNetItemTst == NULL )
290  {
291  if( aMinConn == NOC ) /* Only 1 element in the net. */
292  {
293  msg.Printf( _( "Pin %s (%s) of component %s is unconnected." ),
294  aNetItemRef->m_PinNum,
295  GetChars( GetText( ii ) ),
296  GetChars( cmp_ref ) );
298  aNetItemRef->m_Start,
299  msg,
300  aNetItemRef->m_Start );
301  return;
302  }
303 
304  if( aMinConn == NOD ) /* Nothing driving the net. */
305  {
306  if( aNetItemRef->m_Type == NET_PIN && aNetItemRef->m_Link )
307  cmp_ref = aNetItemRef->GetComponentParent()->GetRef(
308  &aNetItemRef->m_SheetPath );
309 
310  msg.Printf( _( "Pin %s (%s) of component %s is not driven (Net %d)." ),
311  aNetItemRef->m_PinNum,
312  GetChars( GetText( ii ) ),
313  GetChars( cmp_ref ),
314  aNetItemRef->GetNet() );
315  marker->SetData( ERCE_PIN_NOT_DRIVEN,
316  aNetItemRef->m_Start,
317  msg,
318  aNetItemRef->m_Start );
319  return;
320  }
321 
322  if( aDiag == UNC )
323  {
324  msg.Printf( _( "More than 1 pin connected to an UnConnect symbol." ) );
326  aNetItemRef->m_Start,
327  msg,
328  aNetItemRef->m_Start );
329  return;
330  }
331  }
332 
333  if( aNetItemTst ) /* Error between 2 pins */
334  {
335  jj = aNetItemTst->m_ElectricalPinType;
337 
338  if( aDiag == ERR )
339  {
341  errortype = ERCE_PIN_TO_PIN_ERROR;
342  }
343 
344  wxString alt_cmp( "?" );
345 
346  if( aNetItemTst->m_Type == NET_PIN && aNetItemTst->m_Link )
347  alt_cmp = aNetItemTst->GetComponentParent()->GetRef( &aNetItemTst->m_SheetPath );
348 
349  msg.Printf( _( "Pin %s (%s) of component %s is connected to " ),
350  aNetItemRef->m_PinNum,
351  GetChars( GetText( ii ) ),
352  GetChars( cmp_ref ) );
353  marker->SetData( errortype, aNetItemRef->m_Start, msg, aNetItemRef->m_Start );
354  msg.Printf( _( "pin %s (%s) of component %s (net %d)." ),
355  aNetItemTst->m_PinNum,
356  GetChars( GetText( jj ) ),
357  GetChars( alt_cmp ),
358  aNetItemRef->GetNet() );
359  marker->SetAuxiliaryData( msg, aNetItemTst->m_Start );
360  }
361 }
362 
363 
365  unsigned aNetItemRef, unsigned aNetStart,
366  int* aMinConnexion )
367 {
368  unsigned netItemTst = aNetStart;
370  int erc = OK;
371 
372  /* Analysis of the table of connections. */
373  ELECTRICAL_PINTYPE ref_elect_type = aList->GetItem( aNetItemRef )->m_ElectricalPinType;
374  int local_minconn = NOC;
375 
376  if( ref_elect_type == PIN_NC )
377  local_minconn = NPI;
378 
379  /* Test pins connected to NetItemRef */
380  for( ; ; netItemTst++ )
381  {
382  if( aNetItemRef == netItemTst )
383  continue;
384 
385  // We examine only a given net. We stop the search if the net changes
386  if( ( netItemTst >= aList->size() ) // End of list
387  || ( aList->GetItemNet( aNetItemRef ) !=
388  aList->GetItemNet( netItemTst ) ) ) // End of net
389  {
390  /* End net code found: minimum connection test. */
391  if( ( *aMinConnexion < NET_NC ) && ( local_minconn < NET_NC ) )
392  {
393  /* Not connected or not driven pin. */
394  bool seterr = true;
395 
396  if( local_minconn == NOC &&
397  aList->GetItemType( aNetItemRef ) == NET_PIN )
398  {
399  /* This pin is not connected: for multiple part per
400  * package, and duplicated pin,
401  * search for an other instance of this pin
402  * this will be flagged only if all instances of this pin
403  * are not connected
404  * TODO test also if instances connected are connected to
405  * the same net
406  */
407  for( unsigned duplicate = 0; duplicate < aList->size(); duplicate++ )
408  {
409  if( aList->GetItemType( duplicate ) != NET_PIN )
410  continue;
411 
412  if( duplicate == aNetItemRef )
413  continue;
414 
415  if( aList->GetItem( aNetItemRef )->m_PinNum !=
416  aList->GetItem( duplicate )->m_PinNum )
417  continue;
418 
419  if( ( (SCH_COMPONENT*) aList->GetItem( aNetItemRef )->
420  m_Link )->GetRef( &aList->GetItem( aNetItemRef )-> m_SheetPath ) !=
421  ( (SCH_COMPONENT*) aList->GetItem( duplicate )->m_Link )
422  ->GetRef( &aList->GetItem( duplicate )->m_SheetPath ) )
423  continue;
424 
425  // Same component and same pin. Do dot create error for this pin
426  // if the other pin is connected (i.e. if duplicate net has an other
427  // item)
428  if( (duplicate > 0)
429  && ( aList->GetItemNet( duplicate ) ==
430  aList->GetItemNet( duplicate - 1 ) ) )
431  seterr = false;
432 
433  if( (duplicate < aList->size() - 1)
434  && ( aList->GetItemNet( duplicate ) ==
435  aList->GetItemNet( duplicate + 1 ) ) )
436  seterr = false;
437  }
438  }
439 
440  if( seterr )
441  Diagnose( aList->GetItem( aNetItemRef ), NULL, local_minconn, WAR );
442 
443  *aMinConnexion = DRV; // inhibiting other messages of this
444  // type for the net.
445  }
446  return;
447  }
448 
449  switch( aList->GetItemType( netItemTst ) )
450  {
452  case NET_SEGMENT:
453  case NET_BUS:
454  case NET_JUNCTION:
455  case NET_LABEL:
456  case NET_HIERLABEL:
457  case NET_BUSLABELMEMBER:
460  case NET_SHEETLABEL:
461  case NET_GLOBLABEL:
463  case NET_PINLABEL:
464  break;
465 
466  case NET_NOCONNECT:
467  local_minconn = std::max( NET_NC, local_minconn );
468  break;
469 
470  case NET_PIN:
471  jj = aList->GetItem( netItemTst )->m_ElectricalPinType;
472  local_minconn = std::max( MinimalReq[ref_elect_type][jj], local_minconn );
473 
474  if( netItemTst <= aNetItemRef )
475  break;
476 
477  if( erc == OK )
478  {
479  erc = DiagErc[ref_elect_type][jj];
480 
481  if( erc != OK )
482  {
483  if( aList->GetConnectionType( netItemTst ) == UNCONNECTED )
484  {
485  Diagnose( aList->GetItem( aNetItemRef ),
486  aList->GetItem( netItemTst ),
487  0, erc );
488  aList->SetConnectionType( netItemTst, NOCONNECT_SYMBOL_PRESENT );
489  }
490  }
491  }
492 
493  break;
494  }
495  }
496 }
497 
498 int NETLIST_OBJECT_LIST::CountPinsInNet( unsigned aNetStart )
499 {
500  int count = 0;
501  int curr_net = GetItemNet( aNetStart );
502 
503  /* Test pins connected to NetItemRef */
504  for( unsigned item = aNetStart; item < size(); item++ )
505  {
506  // We examine only a given net. We stop the search if the net changes
507  if( curr_net != GetItemNet( item ) ) // End of net
508  break;
509 
510  if( GetItemType( item ) == NET_PIN )
511  count++;
512  }
513 
514  return count;
515 }
516 
517 bool WriteDiagnosticERC( const wxString& aFullFileName )
518 {
519  wxString msg;
520 
521  wxFFile file( aFullFileName, wxT( "wt" ) );
522 
523  if( !file.IsOpened() )
524  return false;
525 
526  msg = _( "ERC report" );
527  msg << wxT(" (") << DateAndTime() << wxT( ", " )
528  << _( "Encoding UTF8" ) << wxT( " )\n" );
529 
530  int err_count = 0;
531  int warn_count = 0;
532  int total_count = 0;
533  SCH_SHEET_LIST sheetList( g_RootSheet );
534 
535  for( unsigned i = 0; i < sheetList.size(); i++ )
536  {
537  msg << wxString::Format( _( "\n***** Sheet %s\n" ),
538  GetChars( sheetList[i].PathHumanReadable() ) );
539 
540  for( SCH_ITEM* item = sheetList[i].LastDrawList(); item != NULL; item = item->Next() )
541  {
542  if( item->Type() != SCH_MARKER_T )
543  continue;
544 
545  SCH_MARKER* marker = (SCH_MARKER*) item;
546 
547  if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
548  continue;
549 
550  total_count++;
551 
553  err_count++;
554 
556  warn_count++;
557 
558  msg << marker->GetReporter().ShowReport();
559  }
560  }
561 
562  msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ),
563  total_count, err_count, warn_count );
564 
565  // Currently: write report using UTF8 (as usual in Kicad).
566  // TODO: see if we can use the current encoding page (mainly for Windows users),
567  // Or other format (HTML?)
568  file.Write( msg );
569 
570  // wxFFile dtor will close the file.
571 
572  return true;
573 }
574 
575 
576 void NETLIST_OBJECT_LIST::TestforNonOrphanLabel( unsigned aNetItemRef, unsigned aStartNet )
577 {
578  unsigned netItemTst = aStartNet;
579  int erc = 1;
580 
581  // Review the list of labels connected to NetItemRef:
582  for( ; ; netItemTst++ )
583  {
584  if( netItemTst == aNetItemRef )
585  continue;
586 
587  /* Is always in the same net? */
588  if( ( netItemTst == size() )
589  || ( GetItemNet( aNetItemRef ) != GetItemNet( netItemTst ) ) )
590  {
591  /* End Netcode found. */
592  if( erc )
593  {
594  /* Glabel or SheetLabel orphaned. */
595  Diagnose( GetItem( aNetItemRef ), NULL, -1, WAR );
596  }
597 
598  return;
599  }
600 
601  if( GetItem( aNetItemRef )->IsLabelConnected( GetItem( netItemTst ) ) )
602  erc = 0;
603 
604  //same thing, different order.
605  if( GetItem( netItemTst )->IsLabelConnected( GetItem( aNetItemRef ) ) )
606  erc = 0;
607  }
608 }
609 
610 
611 // this code try to detect similar labels, i.e. labels which are identical
612 // when they are compared using case insensitive coparisons.
613 
614 
615 // A helper struct to compare NETLIST_OBJECT items by sheetpath and label texts
616 // for a std::set<NETLIST_OBJECT*> container
617 // the full text is "sheetpath+label" for local labels and "label" for global labels
619 {
620  bool operator() ( const NETLIST_OBJECT* lab1, const NETLIST_OBJECT* lab2 )
621  {
622  wxString str1 = lab1->m_SheetPath.Path() + lab1->m_Label;
623  wxString str2 = lab2->m_SheetPath.Path() + lab2->m_Label;
624 
625  return str1.Cmp( str2 ) < 0;
626  }
627 };
628 
630 {
631  bool operator() ( const NETLIST_OBJECT* lab1, const NETLIST_OBJECT* lab2 )
632  {
633  return lab1->m_Label.Cmp( lab2->m_Label ) < 0;
634  }
635 };
636 
638 {
639  bool operator() ( const NETLIST_OBJECT* lab1, const NETLIST_OBJECT* lab2 )
640  {
641  return lab1->m_SheetPath.Path().Cmp( lab2->m_SheetPath.Path() ) < 0;
642  }
643 };
644 
645 // Helper functions to build the warning messages about Similar Labels:
646 static int countIndenticalLabels( std::vector<NETLIST_OBJECT*>& aList, NETLIST_OBJECT* aLabel );
647 static void SimilarLabelsDiagnose( NETLIST_OBJECT* aItemA, NETLIST_OBJECT* aItemB );
648 
649 
651 {
652  // Similar labels which are different when using case sensitive comparisons
653  // but are equal when using case insensitive comparisons
654 
655  // list of all labels (used the better item to build diag messages)
656  std::vector<NETLIST_OBJECT*> fullLabelList;
657  // list of all labels , each label appears only once (used to to detect similar labels)
658  std::set<NETLIST_OBJECT*, compare_labels> uniqueLabelList;
659  wxString msg;
660 
661  // Build a list of differents labels. If inside a given sheet there are
662  // more than one given label, only one label is stored.
663  // not also the sheet labels are not taken in account for 2 reasons:
664  // * they are in the root sheet but they are seen only from the child sheet
665  // * any mismatch between child sheet hierarchical labels and the sheet label
666  // already detected by ERC
667  for( unsigned netItem = 0; netItem < size(); ++netItem )
668  {
669  switch( GetItemType( netItem ) )
670  {
671  case NET_LABEL:
672  case NET_BUSLABELMEMBER:
673  case NET_PINLABEL:
675  case NET_HIERLABEL:
677  case NET_GLOBLABEL:
678  // add this label in lists
679  uniqueLabelList.insert( GetItem( netItem ) );
680  fullLabelList.push_back( GetItem( netItem ) );
681  break;
682 
683  case NET_SHEETLABEL:
685  default:
686  break;
687  }
688  }
689 
690  // build global labels and compare
691  std::set<NETLIST_OBJECT*, compare_label_names> loc_labelList;
692  std::set<NETLIST_OBJECT*>::const_iterator it;
693 
694  for( it = uniqueLabelList.begin(); it != uniqueLabelList.end(); ++it )
695  {
696  if( (*it)->IsLabelGlobal() )
697  loc_labelList.insert( *it );
698  }
699 
700  // compare global labels (same label names appears only once in list)
701  for( it = loc_labelList.begin(); it != loc_labelList.end(); ++it )
702  {
703  std::set<NETLIST_OBJECT*>::const_iterator it_aux = it;
704 
705  for( ++it_aux; it_aux != loc_labelList.end(); ++it_aux )
706  {
707  if( (*it)->m_Label.CmpNoCase( (*it_aux)->m_Label ) == 0 )
708  {
709  // Create new marker for ERC.
710  int cntA = countIndenticalLabels( fullLabelList, *it );
711  int cntB = countIndenticalLabels( fullLabelList, *it_aux );
712 
713  if( cntA <= cntB )
714  SimilarLabelsDiagnose( (*it), (*it_aux) );
715  else
716  SimilarLabelsDiagnose( (*it_aux), (*it) );
717  }
718  }
719  }
720 
721  // Build paths list
722  std::set<NETLIST_OBJECT*, compare_paths> pathsList;
723 
724  for( it = uniqueLabelList.begin(); it != uniqueLabelList.end(); ++it )
725  pathsList.insert( *it );
726 
727  // Examine each label inside a sheet path:
728  for( it = pathsList.begin(); it != pathsList.end(); ++it )
729  {
730  loc_labelList.clear();
731 
732  std::set<NETLIST_OBJECT*>::const_iterator it_aux = uniqueLabelList.begin();
733 
734  for( ; it_aux != uniqueLabelList.end(); ++it_aux )
735  {
736  if( (*it)->m_SheetPath.Path() == (*it_aux)->m_SheetPath.Path() )
737  loc_labelList.insert( *it_aux );
738  }
739 
740  // at this point, loc_labelList contains labels of the current sheet path.
741  // Detect similar labels (same label names appears only once in list)
742  std::set<NETLIST_OBJECT*>::const_iterator ref_it;
743 
744  for( ref_it = loc_labelList.begin(); ref_it != loc_labelList.end(); ++ref_it )
745  {
746  NETLIST_OBJECT* ref_item = *ref_it;
747  it_aux = ref_it;
748 
749  for( ++it_aux; it_aux != loc_labelList.end(); ++it_aux )
750  {
751  // global label versus global label was already examined.
752  // here, at least one label must be local
753  if( ref_item->IsLabelGlobal() && (*it_aux)->IsLabelGlobal() )
754  continue;
755 
756  if( ref_item->m_Label.CmpNoCase( (*it_aux)->m_Label ) == 0 )
757  {
758  // Create new marker for ERC.
759  int cntA = countIndenticalLabels( fullLabelList, ref_item );
760  int cntB = countIndenticalLabels( fullLabelList, *it_aux );
761 
762  if( cntA <= cntB )
763  SimilarLabelsDiagnose( ref_item, (*it_aux) );
764  else
765  SimilarLabelsDiagnose( (*it_aux), ref_item );
766  }
767  }
768  }
769  }
770 }
771 
772 // Helper function: count the number of labels identical to aLabel
773 // for global label: global labels in the full project
774 // for local label: all labels in the current sheet
775 static int countIndenticalLabels( std::vector<NETLIST_OBJECT*>& aList, NETLIST_OBJECT* aLabel )
776 {
777  int count = 0;
778 
779  if( aLabel->IsLabelGlobal() )
780  {
781  for( unsigned netItem = 0; netItem < aList.size(); ++netItem )
782  {
783  NETLIST_OBJECT* item = aList[netItem];
784 
785  if( item->IsLabelGlobal() && item->m_Label == aLabel->m_Label )
786  count++;
787  }
788  }
789  else
790  {
791  for( unsigned netItem = 0; netItem < aList.size(); ++netItem )
792  {
793  NETLIST_OBJECT* item = aList[netItem];
794 
795  if( item->m_Label == aLabel->m_Label &&
796  item->m_SheetPath.Path() == aLabel->m_SheetPath.Path() )
797  count++;
798  }
799  }
800 
801  return count;
802 }
803 
804 // Helper function: creates a marker for similar labels ERC warning
805 static void SimilarLabelsDiagnose( NETLIST_OBJECT* aItemA, NETLIST_OBJECT* aItemB )
806 {
807  // Create new marker for ERC.
808  SCH_MARKER* marker = new SCH_MARKER();
809 
810  marker->SetTimeStamp( GetNewTimeStamp() );
813  SCH_SCREEN* screen = aItemA->m_SheetPath.LastScreen();
814  screen->Append( marker );
815 
816  wxString fmt = aItemA->IsLabelGlobal() ?
817  _( "Global label \"%s\" (sheet \"%s\") looks like:" ) :
818  _( "Local label \"%s\" (sheet \"%s\") looks like:" );
819  wxString msg;
820 
821  msg.Printf( fmt, GetChars( aItemA->m_Label ), GetChars( aItemA->m_SheetPath.PathHumanReadable() ) );
822  marker->SetData( aItemA->IsLabelGlobal() && aItemB->IsLabelGlobal() ?
824  aItemA->m_Start, msg, aItemA->m_Start );
825 
826  fmt = aItemB->IsLabelGlobal() ? _( "Global label \"%s\" (sheet \"%s\")" ) :
827  _( "Local label \"%s\" (sheet \"%s\")" );
828  msg.Printf( fmt, GetChars( aItemB->m_Label ), GetChars( aItemB->m_SheetPath.PathHumanReadable() ) );
829  marker->SetAuxiliaryData( msg, aItemB->m_Start );
830 }
Definition of the SCH_SHEET class for Eeschema.
Class SCH_SHEET_LIST.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:225
bool IsLabelGlobal() const
Function IsLabelGlobal.
SCH_SCREEN * GetNext()
Definition: erc.h:43
bool operator()(const NETLIST_OBJECT *lab1, const NETLIST_OBJECT *lab2)
Definition: erc.cpp:631
#define ERCE_SIMILAR_LABELS
Definition: erc.h:61
void TestOthersItems(NETLIST_OBJECT_LIST *aList, unsigned aNetItemRef, unsigned aNetStart, int *aMinConnexion)
Perform ERC testing for electrical conflicts between NetItemRef and other items (mainly pin) on the s...
Definition: erc.cpp:364
#define ERCE_HIERACHICAL_LABEL
Definition: erc.h:58
NETLIST_ITEM_T GetItemType(unsigned aIdx) const
Acces to an item type.
NETLIST_OBJECT * GetItem(unsigned aIdx) const
Acces to an item in list.
int DiagErc[PINTYPE_COUNT][PINTYPE_COUNT]
Definition: erc.cpp:124
const wxString CommentERC_H[]
Definition: erc.cpp:87
wxString ShowReport() const
Function ShowReport translates this object into a text string suitable for saving to disk in a report...
int CountPinsInNet(unsigned aNetStart)
Counts number of pins connected on the same net.
Definition: erc.cpp:498
#define ERCE_PIN_NOT_DRIVEN
Definition: erc.h:55
void Diagnose(NETLIST_OBJECT *aNetItemRef, NETLIST_OBJECT *aNetItemTst, int aMinConn, int aDiag)
Performs ERC testing and creates an ERC marker to show the ERC problem for aNetItemRef or between aNe...
Definition: erc.cpp:228
#define ERCE_PIN_NOT_CONNECTED
Definition: erc.h:54
SCH_SHEET_PATH m_SheetPath
ELECTRICAL_PINTYPE m_ElectricalPinType
SCH_SCREEN * LastScreen() const
Function LastScreen.
NETLIST_ITEM_T m_Type
SCH_ITEM * Next() const
void SetConnectionType(unsigned aIdx, NET_CONNECTION_T aFlg=UNCONNECTED)
Set the item connection type: UNCONNECTED Pin or Label not connected (error) NOCONNECT_SYMBOL_PRESENT...
void SetData(int aErrorCode, const wxPoint &aMarkerPos, const wxString &aText, const wxPoint &aPos, const wxString &bText, const wxPoint &bPos)
Function SetData fills in all the reportable data associated with a MARKER.
bool WriteDiagnosticERC(const wxString &aFullFileName)
Function WriteDiagnosticERC save the ERC errors to aFullFileName.
Definition: erc.cpp:517
enum TYPEMARKER GetMarkerType() const
Class NETLIST_OBJECT_LIST is a container holding and owning NETLIST_OBJECTs, which are connected item...
wxString Path() const
Function Path the path uses the time stamps which do not changes even when editing sheet parameters a...
#define DRV
Definition: erc.h:66
Definition: erc.h:42
timestamp_t GetNewTimeStamp()
Definition: common.cpp:166
Definition: erc.h:44
bool operator()(const NETLIST_OBJECT *lab1, const NETLIST_OBJECT *lab2)
Definition: erc.cpp:639
MARKER_SEVERITY GetErrorLevel() const
int TestDuplicateSheetNames(bool aCreateMarker)
Function TestDuplicateSheetNames( ) inside a given sheet, one cannot have sheets with duplicate names...
Definition: erc.cpp:177
#define ERCE_SIMILAR_GLBL_LABELS
Definition: erc.h:62
SCH_SHEET * g_RootSheet
Definition: eeschema.cpp:55
void TestforSimilarLabels()
Function TestforSimilarLabels detects labels which are different when using case sensitive comparison...
Definition: erc.cpp:650
#define ERCE_GLOBLABEL
Definition: erc.h:60
#define ERCE_DUPLICATE_SHEET_NAME
Definition: erc.h:53
const DRC_ITEM & GetReporter() const
Function GetReporter returns the DRC_ITEM held within this MARKER so that its interface may be used...
wxString PathHumanReadable() const
Function PathHumanReadable returns the sheet path in a human readable form, i.e.
void TestforNonOrphanLabel(unsigned aNetItemRef, unsigned aStartNet)
Function TestforNonOrphanLabel Sheet labels are expected to be connected to a hierarchical label...
Definition: erc.cpp:576
wxString GetText(GRAPHIC_PINSHAPE shape)
Definition: pin_shape.cpp:33
Definition: erc.h:45
#define ERCE_PIN_TO_PIN_ERROR
Definition: erc.h:57
void SetMarkerType(enum TYPEMARKER aMarkerType)
accessors to set/get marker type (DRC, ERC, or other)
const wxString GetRef(const SCH_SHEET_PATH *aSheet)
Return the reference for the given sheet path.
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
static int countIndenticalLabels(std::vector< NETLIST_OBJECT * > &aList, NETLIST_OBJECT *aLabel)
Definition: erc.cpp:775
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
Definition of the NETLIST_OBJECT class.
bool operator()(const NETLIST_OBJECT *lab1, const NETLIST_OBJECT *lab2)
Definition: erc.cpp:620
SCH_ITEM * GetDrawItems() const
Definition the SCH_COMPONENT class for Eeschema.
#define NOC
Definition: erc.h:69
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
NET_CONNECTION_T GetConnectionType(unsigned aIdx)
static int MinimalReq[PINTYPE_COUNT][PINTYPE_COUNT]
Look up table which gives the minimal drive for a pair of connected pins on a net.
Definition: erc.cpp:160
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
ELECTRICAL_PINTYPE
The component library pin object electrical types used in ERC tests.
Definition: pin_type.h:37
void Append(SCH_ITEM *aItem)
#define max(a, b)
Definition: auxiliary.h:86
void SetErrorLevel(MARKER_SEVERITY aErrorLevel)
accessors to set/get error levels (warning, error, fatal error..)
#define NPI
Definition: erc.h:65
Class SCH_COMPONENT describes a real schematic component.
Definition: sch_component.h:69
int DefaultDiagErc[PINTYPE_COUNT][PINTYPE_COUNT]
Default Look up table which gives the ERC error level for a pair of connected pins Same as DiagErc...
Definition: erc.cpp:133
const wxString CommentERC_V[]
Definition: erc.cpp:103
errortype
Definition: erc.h:40
SCH_SCREEN * GetFirst()
int GetItemNet(unsigned aIdx) const
Acces to an item net code.
#define NET_NC
Definition: erc.h:67
#define ERCE_NOCONNECT_CONNECTED
Definition: erc.h:59
#define ERCE_PIN_TO_PIN_WARNING
Definition: erc.h:56
Class SCH_ITEM is a base class for any item which can be embedded within the SCHEMATIC container clas...
void SetAuxiliaryData(const wxString &aAuxiliaryText, const wxPoint &aAuxiliaryPos)
Function SetAuxiliaryData initialize data for the second (auxiliary) item.
wxString DateAndTime()
Function DateAndTime.
Definition: string.cpp:229
static void SimilarLabelsDiagnose(NETLIST_OBJECT *aItemA, NETLIST_OBJECT *aItemB)
Definition: erc.cpp:805
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
#define NOD
Definition: erc.h:68
void SetTimeStamp(timestamp_t aNewTimeStamp)
Definition: base_struct.h:230