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 string_pinnum, cmp_ref;
285  char ascii_buf[5];
286  ascii_buf[4] = 0;
287  memcpy( ascii_buf, &aNetItemRef->m_PinNum, 4 );
288  string_pinnum = FROM_UTF8( ascii_buf );
289  cmp_ref = wxT( "?" );
290 
291  if( aNetItemRef->m_Type == NET_PIN && aNetItemRef->m_Link )
292  cmp_ref = aNetItemRef->GetComponentParent()->GetRef( &aNetItemRef->m_SheetPath );
293 
294  if( aNetItemTst == NULL )
295  {
296  if( aMinConn == NOC ) /* Only 1 element in the net. */
297  {
298  msg.Printf( _( "Pin %s (%s) of component %s is unconnected." ),
299  GetChars( string_pinnum ),
300  GetChars( GetText( ii ) ),
301  GetChars( cmp_ref ) );
303  aNetItemRef->m_Start,
304  msg,
305  aNetItemRef->m_Start );
306  return;
307  }
308 
309  if( aMinConn == NOD ) /* Nothing driving the net. */
310  {
311  if( aNetItemRef->m_Type == NET_PIN && aNetItemRef->m_Link )
312  cmp_ref = aNetItemRef->GetComponentParent()->GetRef(
313  &aNetItemRef->m_SheetPath );
314 
315  msg.Printf( _( "Pin %s (%s) of component %s is not driven (Net %d)." ),
316  GetChars( string_pinnum ),
317  GetChars( GetText( ii ) ),
318  GetChars( cmp_ref ),
319  aNetItemRef->GetNet() );
320  marker->SetData( ERCE_PIN_NOT_DRIVEN,
321  aNetItemRef->m_Start,
322  msg,
323  aNetItemRef->m_Start );
324  return;
325  }
326 
327  if( aDiag == UNC )
328  {
329  msg.Printf( _( "More than 1 pin connected to an UnConnect symbol." ) );
331  aNetItemRef->m_Start,
332  msg,
333  aNetItemRef->m_Start );
334  return;
335  }
336  }
337 
338  if( aNetItemTst ) /* Error between 2 pins */
339  {
340  jj = aNetItemTst->m_ElectricalPinType;
342 
343  if( aDiag == ERR )
344  {
346  errortype = ERCE_PIN_TO_PIN_ERROR;
347  }
348 
349  wxString alt_string_pinnum, alt_cmp;
350  memcpy( ascii_buf, &aNetItemTst->m_PinNum, 4 );
351  alt_string_pinnum = FROM_UTF8( ascii_buf );
352  alt_cmp = wxT( "?" );
353 
354  if( aNetItemTst->m_Type == NET_PIN && aNetItemTst->m_Link )
355  alt_cmp = aNetItemTst->GetComponentParent()->GetRef( &aNetItemTst->m_SheetPath );
356 
357  msg.Printf( _( "Pin %s (%s) of component %s is connected to " ),
358  GetChars( string_pinnum ),
359  GetChars( GetText( ii ) ),
360  GetChars( cmp_ref ) );
361  marker->SetData( errortype, aNetItemRef->m_Start, msg, aNetItemRef->m_Start );
362  msg.Printf( _( "pin %s (%s) of component %s (net %d)." ),
363  GetChars( alt_string_pinnum ),
364  GetChars( GetText( jj ) ),
365  GetChars( alt_cmp ),
366  aNetItemRef->GetNet() );
367  marker->SetAuxiliaryData( msg, aNetItemTst->m_Start );
368  }
369 }
370 
371 
373  unsigned aNetItemRef, unsigned aNetStart,
374  int* aMinConnexion )
375 {
376  unsigned netItemTst = aNetStart;
378  int erc = OK;
379 
380  /* Analysis of the table of connections. */
381  ELECTRICAL_PINTYPE ref_elect_type = aList->GetItem( aNetItemRef )->m_ElectricalPinType;
382  int local_minconn = NOC;
383 
384  if( ref_elect_type == PIN_NC )
385  local_minconn = NPI;
386 
387  /* Test pins connected to NetItemRef */
388  for( ; ; netItemTst++ )
389  {
390  if( aNetItemRef == netItemTst )
391  continue;
392 
393  // We examine only a given net. We stop the search if the net changes
394  if( ( netItemTst >= aList->size() ) // End of list
395  || ( aList->GetItemNet( aNetItemRef ) !=
396  aList->GetItemNet( netItemTst ) ) ) // End of net
397  {
398  /* End net code found: minimum connection test. */
399  if( ( *aMinConnexion < NET_NC ) && ( local_minconn < NET_NC ) )
400  {
401  /* Not connected or not driven pin. */
402  bool seterr = true;
403 
404  if( local_minconn == NOC &&
405  aList->GetItemType( aNetItemRef ) == NET_PIN )
406  {
407  /* This pin is not connected: for multiple part per
408  * package, and duplicated pin,
409  * search for an other instance of this pin
410  * this will be flagged only if all instances of this pin
411  * are not connected
412  * TODO test also if instances connected are connected to
413  * the same net
414  */
415  for( unsigned duplicate = 0; duplicate < aList->size(); duplicate++ )
416  {
417  if( aList->GetItemType( duplicate ) != NET_PIN )
418  continue;
419 
420  if( duplicate == aNetItemRef )
421  continue;
422 
423  if( aList->GetItem( aNetItemRef )->m_PinNum !=
424  aList->GetItem( duplicate )->m_PinNum )
425  continue;
426 
427  if( ( (SCH_COMPONENT*) aList->GetItem( aNetItemRef )->
428  m_Link )->GetRef( &aList->GetItem( aNetItemRef )-> m_SheetPath ) !=
429  ( (SCH_COMPONENT*) aList->GetItem( duplicate )->m_Link )
430  ->GetRef( &aList->GetItem( duplicate )->m_SheetPath ) )
431  continue;
432 
433  // Same component and same pin. Do dot create error for this pin
434  // if the other pin is connected (i.e. if duplicate net has an other
435  // item)
436  if( (duplicate > 0)
437  && ( aList->GetItemNet( duplicate ) ==
438  aList->GetItemNet( duplicate - 1 ) ) )
439  seterr = false;
440 
441  if( (duplicate < aList->size() - 1)
442  && ( aList->GetItemNet( duplicate ) ==
443  aList->GetItemNet( duplicate + 1 ) ) )
444  seterr = false;
445  }
446  }
447 
448  if( seterr )
449  Diagnose( aList->GetItem( aNetItemRef ), NULL, local_minconn, WAR );
450 
451  *aMinConnexion = DRV; // inhibiting other messages of this
452  // type for the net.
453  }
454  return;
455  }
456 
457  switch( aList->GetItemType( netItemTst ) )
458  {
460  case NET_SEGMENT:
461  case NET_BUS:
462  case NET_JUNCTION:
463  case NET_LABEL:
464  case NET_HIERLABEL:
465  case NET_BUSLABELMEMBER:
468  case NET_SHEETLABEL:
469  case NET_GLOBLABEL:
471  case NET_PINLABEL:
472  break;
473 
474  case NET_NOCONNECT:
475  local_minconn = std::max( NET_NC, local_minconn );
476  break;
477 
478  case NET_PIN:
479  jj = aList->GetItem( netItemTst )->m_ElectricalPinType;
480  local_minconn = std::max( MinimalReq[ref_elect_type][jj], local_minconn );
481 
482  if( netItemTst <= aNetItemRef )
483  break;
484 
485  if( erc == OK )
486  {
487  erc = DiagErc[ref_elect_type][jj];
488 
489  if( erc != OK )
490  {
491  if( aList->GetConnectionType( netItemTst ) == UNCONNECTED )
492  {
493  Diagnose( aList->GetItem( aNetItemRef ),
494  aList->GetItem( netItemTst ),
495  0, erc );
496  aList->SetConnectionType( netItemTst, NOCONNECT_SYMBOL_PRESENT );
497  }
498  }
499  }
500 
501  break;
502  }
503  }
504 }
505 
506 int NETLIST_OBJECT_LIST::CountPinsInNet( unsigned aNetStart )
507 {
508  int count = 0;
509  int curr_net = GetItemNet( aNetStart );
510 
511  /* Test pins connected to NetItemRef */
512  for( unsigned item = aNetStart; item < size(); item++ )
513  {
514  // We examine only a given net. We stop the search if the net changes
515  if( curr_net != GetItemNet( item ) ) // End of net
516  break;
517 
518  if( GetItemType( item ) == NET_PIN )
519  count++;
520  }
521 
522  return count;
523 }
524 
525 bool WriteDiagnosticERC( const wxString& aFullFileName )
526 {
527  wxString msg;
528 
529  wxFFile file( aFullFileName, wxT( "wt" ) );
530 
531  if( !file.IsOpened() )
532  return false;
533 
534  msg = _( "ERC report" );
535  msg << wxT(" (") << DateAndTime() << wxT( ", " )
536  << _( "Encoding UTF8" ) << wxT( " )\n" );
537 
538  int err_count = 0;
539  int warn_count = 0;
540  int total_count = 0;
541  SCH_SHEET_LIST sheetList( g_RootSheet );
542 
543  for( unsigned i = 0; i < sheetList.size(); i++ )
544  {
545  msg << wxString::Format( _( "\n***** Sheet %s\n" ),
546  GetChars( sheetList[i].PathHumanReadable() ) );
547 
548  for( SCH_ITEM* item = sheetList[i].LastDrawList(); item != NULL; item = item->Next() )
549  {
550  if( item->Type() != SCH_MARKER_T )
551  continue;
552 
553  SCH_MARKER* marker = (SCH_MARKER*) item;
554 
555  if( marker->GetMarkerType() != MARKER_BASE::MARKER_ERC )
556  continue;
557 
558  total_count++;
559 
561  err_count++;
562 
564  warn_count++;
565 
566  msg << marker->GetReporter().ShowReport();
567  }
568  }
569 
570  msg << wxString::Format( _( "\n ** ERC messages: %d Errors %d Warnings %d\n" ),
571  total_count, err_count, warn_count );
572 
573  // Currently: write report using UTF8 (as usual in Kicad).
574  // TODO: see if we can use the current encoding page (mainly for Windows users),
575  // Or other format (HTML?)
576  file.Write( msg );
577 
578  // wxFFile dtor will close the file.
579 
580  return true;
581 }
582 
583 
584 void NETLIST_OBJECT_LIST::TestforNonOrphanLabel( unsigned aNetItemRef, unsigned aStartNet )
585 {
586  unsigned netItemTst = aStartNet;
587  int erc = 1;
588 
589  // Review the list of labels connected to NetItemRef:
590  for( ; ; netItemTst++ )
591  {
592  if( netItemTst == aNetItemRef )
593  continue;
594 
595  /* Is always in the same net? */
596  if( ( netItemTst == size() )
597  || ( GetItemNet( aNetItemRef ) != GetItemNet( netItemTst ) ) )
598  {
599  /* End Netcode found. */
600  if( erc )
601  {
602  /* Glabel or SheetLabel orphaned. */
603  Diagnose( GetItem( aNetItemRef ), NULL, -1, WAR );
604  }
605 
606  return;
607  }
608 
609  if( GetItem( aNetItemRef )->IsLabelConnected( GetItem( netItemTst ) ) )
610  erc = 0;
611 
612  //same thing, different order.
613  if( GetItem( netItemTst )->IsLabelConnected( GetItem( aNetItemRef ) ) )
614  erc = 0;
615  }
616 }
617 
618 
619 // this code try to detect similar labels, i.e. labels which are identical
620 // when they are compared using case insensitive coparisons.
621 
622 
623 // A helper struct to compare NETLIST_OBJECT items by sheetpath and label texts
624 // for a std::set<NETLIST_OBJECT*> container
625 // the full text is "sheetpath+label" for local labels and "label" for global labels
627 {
628  bool operator() ( const NETLIST_OBJECT* lab1, const NETLIST_OBJECT* lab2 )
629  {
630  wxString str1 = lab1->m_SheetPath.Path() + lab1->m_Label;
631  wxString str2 = lab2->m_SheetPath.Path() + lab2->m_Label;
632 
633  return str1.Cmp( str2 ) < 0;
634  }
635 };
636 
638 {
639  bool operator() ( const NETLIST_OBJECT* lab1, const NETLIST_OBJECT* lab2 )
640  {
641  return lab1->m_Label.Cmp( lab2->m_Label ) < 0;
642  }
643 };
644 
646 {
647  bool operator() ( const NETLIST_OBJECT* lab1, const NETLIST_OBJECT* lab2 )
648  {
649  return lab1->m_SheetPath.Path().Cmp( lab2->m_SheetPath.Path() ) < 0;
650  }
651 };
652 
653 // Helper functions to build the warning messages about Similar Labels:
654 static int countIndenticalLabels( std::vector<NETLIST_OBJECT*>& aList, NETLIST_OBJECT* aLabel );
655 static void SimilarLabelsDiagnose( NETLIST_OBJECT* aItemA, NETLIST_OBJECT* aItemB );
656 
657 
659 {
660  // Similar labels which are different when using case sensitive comparisons
661  // but are equal when using case insensitive comparisons
662 
663  // list of all labels (used the better item to build diag messages)
664  std::vector<NETLIST_OBJECT*> fullLabelList;
665  // list of all labels , each label appears only once (used to to detect similar labels)
666  std::set<NETLIST_OBJECT*, compare_labels> uniqueLabelList;
667  wxString msg;
668 
669  // Build a list of differents labels. If inside a given sheet there are
670  // more than one given label, only one label is stored.
671  // not also the sheet labels are not taken in account for 2 reasons:
672  // * they are in the root sheet but they are seen only from the child sheet
673  // * any mismatch between child sheet hierarchical labels and the sheet label
674  // already detected by ERC
675  for( unsigned netItem = 0; netItem < size(); ++netItem )
676  {
677  switch( GetItemType( netItem ) )
678  {
679  case NET_LABEL:
680  case NET_BUSLABELMEMBER:
681  case NET_PINLABEL:
683  case NET_HIERLABEL:
685  case NET_GLOBLABEL:
686  // add this label in lists
687  uniqueLabelList.insert( GetItem( netItem ) );
688  fullLabelList.push_back( GetItem( netItem ) );
689  break;
690 
691  case NET_SHEETLABEL:
693  default:
694  break;
695  }
696  }
697 
698  // build global labels and compare
699  std::set<NETLIST_OBJECT*, compare_label_names> loc_labelList;
700  std::set<NETLIST_OBJECT*>::const_iterator it;
701 
702  for( it = uniqueLabelList.begin(); it != uniqueLabelList.end(); ++it )
703  {
704  if( (*it)->IsLabelGlobal() )
705  loc_labelList.insert( *it );
706  }
707 
708  // compare global labels (same label names appears only once in list)
709  for( it = loc_labelList.begin(); it != loc_labelList.end(); ++it )
710  {
711  std::set<NETLIST_OBJECT*>::const_iterator it_aux = it;
712 
713  for( ++it_aux; it_aux != loc_labelList.end(); ++it_aux )
714  {
715  if( (*it)->m_Label.CmpNoCase( (*it_aux)->m_Label ) == 0 )
716  {
717  // Create new marker for ERC.
718  int cntA = countIndenticalLabels( fullLabelList, *it );
719  int cntB = countIndenticalLabels( fullLabelList, *it_aux );
720 
721  if( cntA <= cntB )
722  SimilarLabelsDiagnose( (*it), (*it_aux) );
723  else
724  SimilarLabelsDiagnose( (*it_aux), (*it) );
725  }
726  }
727  }
728 
729  // Build paths list
730  std::set<NETLIST_OBJECT*, compare_paths> pathsList;
731 
732  for( it = uniqueLabelList.begin(); it != uniqueLabelList.end(); ++it )
733  pathsList.insert( *it );
734 
735  // Examine each label inside a sheet path:
736  for( it = pathsList.begin(); it != pathsList.end(); ++it )
737  {
738  loc_labelList.clear();
739 
740  std::set<NETLIST_OBJECT*>::const_iterator it_aux = uniqueLabelList.begin();
741 
742  for( ; it_aux != uniqueLabelList.end(); ++it_aux )
743  {
744  if( (*it)->m_SheetPath.Path() == (*it_aux)->m_SheetPath.Path() )
745  loc_labelList.insert( *it_aux );
746  }
747 
748  // at this point, loc_labelList contains labels of the current sheet path.
749  // Detect similar labels (same label names appears only once in list)
750  std::set<NETLIST_OBJECT*>::const_iterator ref_it;
751 
752  for( ref_it = loc_labelList.begin(); ref_it != loc_labelList.end(); ++ref_it )
753  {
754  NETLIST_OBJECT* ref_item = *ref_it;
755  it_aux = ref_it;
756 
757  for( ++it_aux; it_aux != loc_labelList.end(); ++it_aux )
758  {
759  // global label versus global label was already examined.
760  // here, at least one label must be local
761  if( ref_item->IsLabelGlobal() && (*it_aux)->IsLabelGlobal() )
762  continue;
763 
764  if( ref_item->m_Label.CmpNoCase( (*it_aux)->m_Label ) == 0 )
765  {
766  // Create new marker for ERC.
767  int cntA = countIndenticalLabels( fullLabelList, ref_item );
768  int cntB = countIndenticalLabels( fullLabelList, *it_aux );
769 
770  if( cntA <= cntB )
771  SimilarLabelsDiagnose( ref_item, (*it_aux) );
772  else
773  SimilarLabelsDiagnose( (*it_aux), ref_item );
774  }
775  }
776  }
777  }
778 }
779 
780 // Helper function: count the number of labels identical to aLabel
781 // for global label: global labels in the full project
782 // for local label: all labels in the current sheet
783 static int countIndenticalLabels( std::vector<NETLIST_OBJECT*>& aList, NETLIST_OBJECT* aLabel )
784 {
785  int count = 0;
786 
787  if( aLabel->IsLabelGlobal() )
788  {
789  for( unsigned netItem = 0; netItem < aList.size(); ++netItem )
790  {
791  NETLIST_OBJECT* item = aList[netItem];
792 
793  if( item->IsLabelGlobal() && item->m_Label == aLabel->m_Label )
794  count++;
795  }
796  }
797  else
798  {
799  for( unsigned netItem = 0; netItem < aList.size(); ++netItem )
800  {
801  NETLIST_OBJECT* item = aList[netItem];
802 
803  if( item->m_Label == aLabel->m_Label &&
804  item->m_SheetPath.Path() == aLabel->m_SheetPath.Path() )
805  count++;
806  }
807  }
808 
809  return count;
810 }
811 
812 // Helper function: creates a marker for similar labels ERC warning
813 static void SimilarLabelsDiagnose( NETLIST_OBJECT* aItemA, NETLIST_OBJECT* aItemB )
814 {
815  // Create new marker for ERC.
816  SCH_MARKER* marker = new SCH_MARKER();
817 
818  marker->SetTimeStamp( GetNewTimeStamp() );
821  SCH_SCREEN* screen = aItemA->m_SheetPath.LastScreen();
822  screen->Append( marker );
823 
824  wxString fmt = aItemA->IsLabelGlobal() ?
825  _( "Global label '%s' (sheet '%s') looks like:" ) :
826  _( "Local label '%s' (sheet '%s') looks like:" );
827  wxString msg;
828 
829  msg.Printf( fmt, GetChars( aItemA->m_Label ), GetChars( aItemA->m_SheetPath.PathHumanReadable() ) );
830  marker->SetData( aItemA->IsLabelGlobal() && aItemB->IsLabelGlobal() ?
832  aItemA->m_Start, msg, aItemA->m_Start );
833 
834  fmt = aItemB->IsLabelGlobal() ? _( "Global label '%s' (sheet '%s')" ) :
835  _( "Local label '%s' (sheet '%s')" );
836  msg.Printf( fmt, GetChars( aItemB->m_Label ), GetChars( aItemB->m_SheetPath.PathHumanReadable() ) );
837  marker->SetAuxiliaryData( msg, aItemB->m_Start );
838 }
Definition of the SCH_SHEET class for Eeschema.
Class SCH_SHEET_LIST.
KICAD_T Type() const
Function Type()
Definition: base_struct.h:198
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:639
#define ERCE_SIMILAR_LABELS
Definition: erc.h:61
static wxString FROM_UTF8(const char *cstring)
function FROM_UTF8 converts a UTF8 encoded C string to a wxString for all wxWidgets build modes...
Definition: macros.h:53
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:372
#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:506
#define ERCE_PIN_NOT_DRIVEN
Definition: erc.h:55
time_t GetNewTimeStamp()
Definition: common.cpp:166
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.
void SetTimeStamp(time_t aNewTimeStamp)
Definition: base_struct.h:203
bool WriteDiagnosticERC(const wxString &aFullFileName)
Function WriteDiagnosticERC save the ERC errors to aFullFileName.
Definition: erc.cpp:525
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
Definition: erc.h:44
bool operator()(const NETLIST_OBJECT *lab1, const NETLIST_OBJECT *lab2)
Definition: erc.cpp:647
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:54
void TestforSimilarLabels()
Function TestforSimilarLabels detects labels which are different when using case sensitive comparison...
Definition: erc.cpp:658
#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:584
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)
SCH_COMPONENT * GetComponentParent() const
For Pins (NET_PINS):
static int countIndenticalLabels(std::vector< NETLIST_OBJECT * > &aList, NETLIST_OBJECT *aLabel)
Definition: erc.cpp:783
Class SCH_SHEET is the sheet symbol placed in a schematic, and is the entry point for a sub schematic...
Definition: sch_sheet.h:216
Definition of the NETLIST_OBJECT class.
bool operator()(const NETLIST_OBJECT *lab1, const NETLIST_OBJECT *lab2)
Definition: erc.cpp:628
SCH_ITEM * GetDrawItems() const
Function GetDrawItems().
const wxString GetRef(const SCH_SHEET_PATH *sheet)
Function GetRef returns the reference, for the given sheet path.
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:813
Class SCH_SCREENS is a container class that holds multiple SCH_SCREENs in a hierarchy.
#define NOD
Definition: erc.h:68