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/arrstr.h>
33#include <wx/filename.h>
34
35#include <algorithm>
36
37
38namespace KICAD_DIFF
39{
40
41SYM_LIB_DIFFER::SYM_LIB_DIFFER( const SYMBOL_MAP& aBefore, const SYMBOL_MAP& aAfter, const wxString& aPath ) :
42 m_before( aBefore ),
43 m_after( aAfter ),
44 m_path( aPath )
45{
46}
47
48
50
51
52std::pair<std::vector<std::unique_ptr<LIB_SYMBOL>>, SYM_LIB_DIFFER::SYMBOL_MAP>
53SYM_LIB_DIFFER::LoadLibrary( const wxString& aPath )
54{
55 std::vector<std::unique_ptr<LIB_SYMBOL>> owners;
56 SYMBOL_MAP map;
57
58 // The s-expression library plugin asserts on relative paths; absolute the
59 // input so CLI callers can pass `./` paths without crashing.
60 wxFileName absPath( aPath );
61 absPath.MakeAbsolute();
62 wxString absStr = absPath.GetFullPath();
63
65 std::vector<LIB_SYMBOL*> rawList;
66
67 // EnumerateSymbolLib can throw IO_ERROR / std::exception on a corrupt or
68 // unreadable file. Let the exception escape so callers can distinguish a
69 // load failure from a deliberately-empty library; swallowing here was
70 // misreported as a valid empty diff downstream.
71 io.EnumerateSymbolLib( rawList, absStr, nullptr );
72
73 // EnumerateSymbolLib returns pointers owned by the IO plugin's cache,
74 // which is destroyed when the SCH_IO_KICAD_SEXPR goes out of scope. Clone
75 // each symbol so we own a stable copy.
76 for( LIB_SYMBOL* sym : rawList )
77 {
78 if( !sym )
79 continue;
80
81 std::unique_ptr<LIB_SYMBOL> owner( new LIB_SYMBOL( *sym ) );
82 map[owner->GetName()] = owner.get();
83 owners.push_back( std::move( owner ) );
84 }
85
86 // The clones still point their parent at the IO cache, which dies with the
87 // plugin below. Re-link derived symbols to the copies we own so Flatten can
88 // still pull in the inherited pins and fields.
89 std::map<wxString, LIB_SYMBOL*> byName;
90
91 for( const std::unique_ptr<LIB_SYMBOL>& owner : owners )
92 byName[owner->GetName()] = owner.get();
93
94 for( const std::unique_ptr<LIB_SYMBOL>& owner : owners )
95 {
96 if( owner->GetParentName().IsEmpty() )
97 continue;
98
99 auto it = byName.find( owner->GetParentName() );
100
101 if( it != byName.end() )
102 owner->SetParent( it->second );
103 }
104
105 return { std::move( owners ), std::move( map ) };
106}
107
108
109namespace
110{
111 // Compare also sees symbol state that no property or element carries
112 constexpr int SYMBOL_COMPARE_FLAGS = ~( SCH_ITEM::COMPARE_FLAGS::UUID | SCH_ITEM::COMPARE_FLAGS::IDENTITY );
113
114
115 std::vector<PROPERTY_DELTA> symbolDeltas( const LIB_SYMBOL* aBefore, const LIB_SYMBOL* aAfter )
116 {
117 std::vector<PROPERTY_DELTA> deltas = DiffItemProperties( aBefore, aAfter );
118 wxString before = wxJoin( aBefore->GetFPFilters(), ' ' );
119 wxString after = wxJoin( aAfter->GetFPFilters(), ' ' );
120
121 if( before != after )
122 {
123 deltas.push_back(
124 { wxS( "Footprint Filters" ), DIFF_VALUE::FromString( before ), DIFF_VALUE::FromString( after ) } );
125 }
126
127 // Unit and body style names are not properties, so a merge could drop one unnoticed
128 auto unitNames = []( const LIB_SYMBOL* aSymbol )
129 {
130 wxArrayString names;
131
132 for( const auto& [unit, name] : aSymbol->GetUnitDisplayNames() )
133 names.Add( wxString::Format( wxS( "%d: %s" ), unit, name ) );
134
135 return wxJoin( names, ',' );
136 };
137
138 before = unitNames( aBefore );
139 after = unitNames( aAfter );
140
141 if( before != after )
142 {
143 deltas.push_back(
144 { wxS( "Unit Names" ), DIFF_VALUE::FromString( before ), DIFF_VALUE::FromString( after ) } );
145 }
146
147 auto bodyStyleNames = []( const LIB_SYMBOL* aSymbol )
148 {
149 wxArrayString names;
150
151 for( const wxString& name : aSymbol->GetBodyStyleNames() )
152 names.Add( name );
153
154 return wxJoin( names, ',' );
155 };
156
157 before = bodyStyleNames( aBefore );
158 after = bodyStyleNames( aAfter );
159
160 if( before != after )
161 {
162 deltas.push_back( { wxS( "Body Style Names" ), DIFF_VALUE::FromString( before ),
163 DIFF_VALUE::FromString( after ) } );
164 }
165
166 return deltas;
167 }
168
169
170 bool hasSymbolDifferences( const LIB_SYMBOL* aBefore, const LIB_SYMBOL* aAfter )
171 {
172 std::unique_ptr<LIB_SYMBOL> before = aBefore->Flatten();
173 std::unique_ptr<LIB_SYMBOL> after = aAfter->Flatten();
174
175 return before->Compare( *after, SYMBOL_COMPARE_FLAGS ) != 0
176 || !symbolDeltas( before.get(), after.get() ).empty()
177 || !DiffSymbolElements( before.get(), after.get() ).empty();
178 }
179} // namespace
180
181
183{
185 m_before, m_after, m_path, wxS( "kicad_sym" ), wxS( "LIB_SYMBOL" ), m_options,
186 []( const LIB_SYMBOL* aSym )
187 {
188 return aSym->GetUnitBoundingBox( 0, 0 );
189 },
190 []( const LIB_SYMBOL* aA, const LIB_SYMBOL* aB )
191 {
192 return hasSymbolDifferences( aA, aB );
193 } );
194
195 for( ITEM_CHANGE& change : result.changes )
196 {
197 if( change.kind != CHANGE_KIND::MODIFIED || !change.refdes )
198 continue;
199
200 auto beforeIt = m_before.find( *change.refdes );
201 auto afterIt = m_after.find( *change.refdes );
202
203 if( beforeIt == m_before.end() || afterIt == m_after.end() )
204 continue;
205
206 std::unique_ptr<LIB_SYMBOL> before = beforeIt->second->Flatten();
207 std::unique_ptr<LIB_SYMBOL> after = afterIt->second->Flatten();
208
209 change.properties = symbolDeltas( before.get(), after.get() );
210 AppendElementChanges( change, DiffSymbolElements( before.get(), after.get() ) );
211 }
212
213 return result;
214}
215
216
217bool SYM_LIB_DIFFER::MergeKeepsBothSides( const SYMBOL_MAP& aAncestor, const SYMBOL_MAP& aOurs,
218 const SYMBOL_MAP& aTheirs, const SYMBOL_MAP& aMerged )
219{
220 auto carries = [&]( const SYMBOL_MAP& aSide, const SYMBOL_MAP& aOther )
221 {
222 std::map<KIID_PATH, ITEM_CHANGE> applied;
223 std::map<KIID_PATH, ITEM_CHANGE> wanted;
224
225 for( ITEM_CHANGE& change : SYM_LIB_DIFFER( aSide, aMerged ).Diff().changes )
226 applied.emplace( change.id, std::move( change ) );
227
228 for( ITEM_CHANGE& change : SYM_LIB_DIFFER( aAncestor, aOther ).Diff().changes )
229 wanted.emplace( change.id, std::move( change ) );
230
231 // The bounding box only places a change on screen, and comes from a different symbol on each side
232 return std::equal( applied.begin(), applied.end(), wanted.begin(), wanted.end(),
233 []( const auto& aA, const auto& aB )
234 {
235 return aA.first == aB.first && aA.second.kind == aB.second.kind
236 && aA.second.properties == aB.second.properties
237 && aA.second.children == aB.second.children;
238 } );
239 };
240
241 return carries( aOurs, aTheirs ) && carries( aTheirs, aOurs );
242}
243
244} // namespace KICAD_DIFF
const char * name
static DIFF_VALUE FromString(const wxString &aValue)
const SYMBOL_MAP & m_before
static bool MergeKeepsBothSides(const SYMBOL_MAP &aAncestor, const SYMBOL_MAP &aOurs, const SYMBOL_MAP &aTheirs, const SYMBOL_MAP &aMerged)
True when aMerged differs from each side of a three-way merge exactly as the other side differs from ...
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:114
const BOX2I GetUnitBoundingBox(int aUnit, int aBodyStyle, bool aIgnoreHiddenFields=true, bool aIgnoreLabelsOnInvisiblePins=true) const
Get the bounding box for the symbol.
wxArrayString GetFPFilters() const
Definition lib_symbol.h:254
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< 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.
void AppendElementChanges(ITEM_CHANGE &aChange, const std::vector< LIB_ELEMENT > &aElements)
Add each element to aChange as a child change, which is how a library item reports its elements.
std::vector< LIB_ELEMENT > DiffSymbolElements(const LIB_SYMBOL *aBefore, const LIB_SYMBOL *aAfter)
Compares the elements of two library symbols.
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
wxString result
Test unit parsing edge cases and error handling.