KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sym_lib_differ.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#include "sym_lib_differ.h"
25
28#include <lib_symbol.h>
29#include <sch_item.h>
31
32#include <wx/filename.h>
33
34
35namespace KICAD_DIFF
36{
37
38SYM_LIB_DIFFER::SYM_LIB_DIFFER( const SYMBOL_MAP& aBefore, const SYMBOL_MAP& aAfter, const wxString& aPath ) :
39 m_before( aBefore ),
40 m_after( aAfter ),
41 m_path( aPath )
42{
43}
44
45
47
48
49std::pair<std::vector<std::unique_ptr<LIB_SYMBOL>>, SYM_LIB_DIFFER::SYMBOL_MAP>
50SYM_LIB_DIFFER::LoadLibrary( const wxString& aPath )
51{
52 std::vector<std::unique_ptr<LIB_SYMBOL>> owners;
53 SYMBOL_MAP map;
54
55 // The s-expression library plugin asserts on relative paths; absolute the
56 // input so CLI callers can pass `./` paths without crashing.
57 wxFileName absPath( aPath );
58 absPath.MakeAbsolute();
59 wxString absStr = absPath.GetFullPath();
60
62 std::vector<LIB_SYMBOL*> rawList;
63
64 // EnumerateSymbolLib can throw IO_ERROR / std::exception on a corrupt or
65 // unreadable file. Let the exception escape so callers can distinguish a
66 // load failure from a deliberately-empty library; swallowing here was
67 // misreported as a valid empty diff downstream.
68 io.EnumerateSymbolLib( rawList, absStr, nullptr );
69
70 // EnumerateSymbolLib returns pointers owned by the IO plugin's cache,
71 // which is destroyed when the SCH_IO_KICAD_SEXPR goes out of scope. Clone
72 // each symbol so we own a stable copy.
73 for( LIB_SYMBOL* sym : rawList )
74 {
75 if( !sym )
76 continue;
77
78 std::unique_ptr<LIB_SYMBOL> owner( new LIB_SYMBOL( *sym ) );
79 map[owner->GetName()] = owner.get();
80 owners.push_back( std::move( owner ) );
81 }
82
83 // The clones still point their parent at the IO cache, which dies with the
84 // plugin below. Re-link derived symbols to the copies we own so Flatten can
85 // still pull in the inherited pins and fields.
86 std::map<wxString, LIB_SYMBOL*> byName;
87
88 for( const std::unique_ptr<LIB_SYMBOL>& owner : owners )
89 byName[owner->GetName()] = owner.get();
90
91 for( const std::unique_ptr<LIB_SYMBOL>& owner : owners )
92 {
93 if( owner->GetParentName().IsEmpty() )
94 continue;
95
96 auto it = byName.find( owner->GetParentName() );
97
98 if( it != byName.end() )
99 owner->SetParent( it->second );
100 }
101
102 return { std::move( owners ), std::move( map ) };
103}
104
105
106namespace
107{
108 bool hasSymbolDifferences( const LIB_SYMBOL* aBefore, const LIB_SYMBOL* aAfter )
109 {
110 std::unique_ptr<LIB_SYMBOL> before = aBefore->Flatten();
111 std::unique_ptr<LIB_SYMBOL> after = aAfter->Flatten();
112
113 return !DiffItemProperties( before.get(), after.get() ).empty()
114 || !DiffSymbolElements( before.get(), after.get() ).empty();
115 }
116} // namespace
117
118
120{
122 m_before, m_after, m_path, wxS( "kicad_sym" ), wxS( "LIB_SYMBOL" ), m_options,
123 []( const LIB_SYMBOL* aSym )
124 {
125 return aSym->GetUnitBoundingBox( 0, 0 );
126 },
127 []( const LIB_SYMBOL* aA, const LIB_SYMBOL* aB )
128 {
129 return hasSymbolDifferences( aA, aB );
130 } );
131
132 for( ITEM_CHANGE& change : result.changes )
133 {
134 if( change.kind != CHANGE_KIND::MODIFIED || !change.refdes )
135 continue;
136
137 auto beforeIt = m_before.find( *change.refdes );
138 auto afterIt = m_after.find( *change.refdes );
139
140 if( beforeIt == m_before.end() || afterIt == m_after.end() )
141 continue;
142
143 std::unique_ptr<LIB_SYMBOL> before = beforeIt->second->Flatten();
144 std::unique_ptr<LIB_SYMBOL> after = afterIt->second->Flatten();
145
146 change.properties = DiffItemProperties( before.get(), after.get() );
147
148 for( const SYM_ELEMENT& element : DiffSymbolElements( before.get(), after.get() ) )
149 {
150 ITEM_CHANGE child;
151 child.typeName = element.typeName;
152 child.kind = element.kind;
153 child.properties = element.deltas;
154
155 if( !element.key.IsEmpty() )
156 child.refdes = element.key;
157
158 change.children.push_back( std::move( child ) );
159 }
160 }
161
162 return result;
163}
164
165} // namespace KICAD_DIFF
const SYMBOL_MAP & m_before
SYM_LIB_DIFFER(const SYMBOL_MAP &aBefore, const SYMBOL_MAP &aAfter, const wxString &aPath=wxEmptyString)
const SYMBOL_MAP & m_after
DOCUMENT_DIFF Diff() override
Produce a DOCUMENT_DIFF of the inputs the concrete differ was constructed with.
static std::pair< std::vector< std::unique_ptr< LIB_SYMBOL > >, SYMBOL_MAP > LoadLibrary(const wxString &aPath)
Convenience: load a .kicad_sym path into a SYMBOL_MAP using SCH_IO_KICAD_SEXPR::EnumerateSymbolLib.
std::map< wxString, const LIB_SYMBOL * > SYMBOL_MAP
Library content is a map of (canonical_name -> LIB_SYMBOL*).
Define a library symbol object.
Definition lib_symbol.h:79
const BOX2I GetUnitBoundingBox(int aUnit, int aBodyStyle, bool aIgnoreHiddenFields=true, bool aIgnoreLabelsOnInvisiblePins=true) const
Get the bounding box for the symbol.
std::unique_ptr< LIB_SYMBOL > Flatten() const
Return a flattened symbol inheritance to the caller.
A SCH_IO derivation for loading schematic files using the new s-expression file format.
void EnumerateSymbolLib(wxArrayString &aSymbolNameList, const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Populate a list of LIB_SYMBOL alias names contained within the library aLibraryPath.
std::vector< SYM_ELEMENT > DiffSymbolElements(const LIB_SYMBOL *aBefore, const LIB_SYMBOL *aAfter)
Compares the elements of two library symbols.
std::vector< PROPERTY_DELTA > DiffItemProperties(const INSPECTABLE *aBefore, const INSPECTABLE *aAfter)
Enumerate the property deltas between two items of the same dynamic type.
DOCUMENT_DIFF DiffLibraryByName(const MAP &aBefore, const MAP &aAfter, const wxString &aPath, const wxString &aDocType, const wxString &aTypeName, const KICAD_DIFFER::OPTIONS &aOptions, BBoxFn aBBox, ChangedFn aChanged)
Shared name-keyed library diff used by FP_LIB_DIFFER and SYM_LIB_DIFFER.
The full set of changes between two parsed documents of one type.
One change record on a single item.
std::vector< PROPERTY_DELTA > properties
std::optional< wxString > refdes
std::vector< ITEM_CHANGE > children
wxString result
Test unit parsing edge cases and error handling.