KiCad PCB EDA Suite
Loading...
Searching...
No Matches
erc_report.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <wx/ffile.h>
21#include <wx/string.h>
22
23#include <locale_io.h>
24#include <sch_screen.h>
25#include <sch_marker.h>
26#include <schematic.h>
27#include <string_utils.h>
28#include <build_version.h>
29#include "erc_report.h"
30#include <eda_units.h>
31#include <erc/erc.h>
32#include <fstream>
33#include <macros.h>
34#include <json_common.h>
35#include <rc_json_schema.h>
37
38
39ERC_REPORT::ERC_REPORT( SCHEMATIC* aSchematic, EDA_UNITS aReportUnits,
40 std::shared_ptr<RC_ITEMS_PROVIDER> aMarkersProvider ) :
41 m_sch( aSchematic ),
42 m_reportUnits( aReportUnits ),
43 m_markersProvider( std::move( aMarkersProvider ) ),
45{
47 {
48 // When no provider is supplied, fall back to creating one with default severities.
49 // This allows test code to get a basic report without needing to set up a provider.
50 m_markersProvider = std::make_shared<SHEETLIST_ERC_ITEMS_PROVIDER>( m_sch );
52 }
53
54 m_reportedSeverities = m_markersProvider->GetSeverities();
55}
56
57
59{
60 // We need the global LOCALE_IO here in order to
61 // write the report in the c-locale.
62 LOCALE_IO locale;
63 UNITS_PROVIDER unitsProvider( schIUScale, m_reportUnits );
64
65 wxString msg = wxString::Format( wxT( "ERC report (%s, Encoding UTF8)\n" ),
67
68 msg += wxString::Format( wxT( "Report includes: %s\n" ),
70
71 std::map<KIID, EDA_ITEM*> itemMap;
72
73 int err_count = 0;
74 int warn_count = 0;
75 int total_count = 0;
76 SCH_SHEET_LIST sheetList = m_sch->BuildSheetListSortedByPageNumbers();
77
78 sheetList.FillItemMap( itemMap );
79
80 ERC_SETTINGS& settings = m_sch->ErcSettings();
81
82 std::map<SCH_SHEET_PATH, std::vector<ERC_ITEM*>> orderedItems;
83
84 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
85 {
86 if( auto item = dynamic_cast<ERC_ITEM*>( m_markersProvider->GetItem( i ).get() ) )
87 {
88 if( item->MainItemHasSheetPath() )
89 orderedItems[item->GetMainItemSheetPath()].emplace_back( item );
90 else
91 orderedItems[sheetList[0]].emplace_back( item );
92 }
93 }
94
95 for( unsigned i = 0; i < sheetList.size(); i++ )
96 {
97 msg << wxString::Format( wxT( "\n***** Sheet %s\n" ), sheetList[i].PathHumanReadable() );
98
99 for( ERC_ITEM* item : orderedItems[sheetList[i]] )
100 {
101 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
102
103 total_count++;
104
105 switch( severity )
106 {
107 case RPT_SEVERITY_ERROR: err_count++; break;
108 case RPT_SEVERITY_WARNING: warn_count++; break;
109 default: break;
110 }
111
112 msg << item->ShowReport( &unitsProvider, severity, itemMap );
113 }
114 }
115
116 msg << wxString::Format( wxT( "\n ** ERC messages: %d Errors %d Warnings %d\n" ),
117 total_count,
118 err_count,
119 warn_count );
120
121 msg << wxT( "\n ** Ignored checks:\n" );
122
123 bool hasIgnored = false;
124
125 for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
126 {
127 int code = item.GetErrorCode();
128
129 if( code > 0 && settings.GetSeverity( code ) == RPT_SEVERITY_IGNORE )
130 {
131 msg << wxString::Format( wxT( " - %s\n" ), item.GetErrorMessage( false ) );
132 hasIgnored = true;
133 }
134 }
135
136 if( !hasIgnored )
137 msg << wxT( " - " ) << wxT( "None" ) << wxT( "\n" );
138
139 return msg;
140}
141
142
143bool ERC_REPORT::WriteTextReport( const wxString& aFullFileName )
144{
145 wxFFile file( aFullFileName, wxT( "wt" ) );
146
147 if( !file.IsOpened() )
148 return false;
149
150 file.Write( GetTextReport() );
151
152 // wxFFile dtor will close the file.
153 return true;
154}
155
156
157bool ERC_REPORT::WriteJsonReport( const wxString& aFullFileName )
158{
159 std::ofstream jsonFileStream( aFullFileName.fn_str() );
160
161 UNITS_PROVIDER unitsProvider( pcbIUScale, m_reportUnits );
162 std::map<KIID, EDA_ITEM*> itemMap;
163
164 RC_JSON::ERC_REPORT reportHead;
165 wxFileName fn( m_sch->GetFileName() );
166 reportHead.$schema = "https://schemas.kicad.org/erc.v1.json";
167 reportHead.source = fn.GetFullName();
168 reportHead.date = GetISO8601CurrentDateTime();
171
172 // Document which severities are included in this report
174 reportHead.included_severities.push_back( wxS( "error" ) );
175
177 reportHead.included_severities.push_back( wxS( "warning" ) );
178
180 reportHead.included_severities.push_back( wxS( "exclusion" ) );
181
182 SCH_SHEET_LIST sheetList = m_sch->Hierarchy();
183 sheetList.FillItemMap( itemMap );
184
185 ERC_SETTINGS& settings = m_sch->ErcSettings();
186
187 std::map<SCH_SHEET_PATH, std::vector<ERC_ITEM*>> orderedItems;
188
189 for( int i = 0; i < m_markersProvider->GetCount(); ++i )
190 {
191 if( auto item = dynamic_cast<ERC_ITEM*>( m_markersProvider->GetItem( i ).get() ) )
192 {
193 if( item->MainItemHasSheetPath() )
194 orderedItems[item->GetMainItemSheetPath()].emplace_back( item );
195 else
196 orderedItems[sheetList[0]].emplace_back( item );
197 }
198 }
199
200 for( unsigned i = 0; i < sheetList.size(); i++ )
201 {
202 RC_JSON::ERC_SHEET jsonSheet;
203 jsonSheet.path = sheetList[i].PathHumanReadable();
204 jsonSheet.uuid_path = sheetList[i].Path().AsString();
205
206 for( ERC_ITEM* item : orderedItems[sheetList[i]] )
207 {
208 SEVERITY severity = settings.GetSeverity( item->GetErrorCode() );
209
210 RC_JSON::VIOLATION violation;
211 item->GetJsonViolation( violation, &unitsProvider, severity, itemMap );
212
213 jsonSheet.violations.push_back( violation );
214 }
215
216 reportHead.sheets.push_back( jsonSheet );
217 }
218
219 for( const RC_ITEM& item : ERC_ITEM::GetItemsWithSeverities() )
220 {
221 int code = item.GetErrorCode();
222
223 if( code > 0 && settings.GetSeverity( code ) == RPT_SEVERITY_IGNORE )
224 {
225 RC_JSON::IGNORED_CHECK ignoredCheck;
226 ignoredCheck.key = item.GetSettingsKey();
227 ignoredCheck.description = item.GetErrorMessage( false );
228 reportHead.ignored_checks.push_back( ignoredCheck );
229 }
230 }
231
232 nlohmann::json saveJson = nlohmann::json( reportHead );
233 jsonFileStream << std::setw( 4 ) << saveJson << std::endl;
234 jsonFileStream.flush();
235 jsonFileStream.close();
236
237 return true;
238}
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
wxString GetMajorMinorPatchVersion()
Get the major, minor and patch version in a string major.minor.patch This is extracted by CMake from ...
static std::vector< std::reference_wrapper< RC_ITEM > > GetItemsWithSeverities()
Definition erc_item.h:76
EDA_UNITS m_reportUnits
Definition erc_report.h:59
ERC_REPORT(SCHEMATIC *aSchematic, EDA_UNITS aReportUnits, std::shared_ptr< RC_ITEMS_PROVIDER > aMarkersProvider=nullptr)
int m_reportedSeverities
Definition erc_report.h:61
wxString GetTextReport()
Returns the ERC report in "text" (human readable) format in the C-locale.
SCHEMATIC * m_sch
Definition erc_report.h:58
bool WriteJsonReport(const wxString &aFullFileName)
Writes a JSON formatted ERC Report to the given file path in the c-locale.
std::shared_ptr< RC_ITEMS_PROVIDER > m_markersProvider
Definition erc_report.h:60
bool WriteTextReport(const wxString &aFullFileName)
Writes the text report also available via GetTextReport directly to a given file path.
Container for ERC settings.
SEVERITY GetSeverity(int aErrorCode) const
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition locale_io.h:41
A holder for a rule check item, DRC in Pcbnew or ERC in Eeschema.
Definition rc_item.h:81
Holds all the data relating to one schematic.
Definition schematic.h:88
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void FillItemMap(std::map< KIID, EDA_ITEM * > &aMap)
Fill an item cache for temporary use when many items need to be fetched.
EDA_UNITS
Definition eda_units.h:48
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API wxString GetLabel(EDA_UNITS aUnits, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Get the units string for a given units type.
STL namespace.
wxString formatSeverities(int aSeverities)
Convert a severity mask to a human-readable comma-separated string.
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
wxString GetISO8601CurrentDateTime()
std::vector< wxString > included_severities
std::vector< ERC_SHEET > sheets
std::vector< IGNORED_CHECK > ignored_checks