@@ -33,6 +33,16 @@ static bool isCppcheckPremium(const Settings& settings) {
3333 return (settings.cppcheckCfgProductName .compare (0 , 16 , " Cppcheck Premium" ) == 0 );
3434}
3535
36+ static int getMisraCVersion (const Settings& settings) {
37+ if (settings.premiumArgs .find (" misra-c-2012" ) != std::string::npos)
38+ return 2012 ;
39+ if (settings.premiumArgs .find (" misra-c-2023" ) != std::string::npos)
40+ return 2023 ;
41+ if (settings.addons .count (" misra" ))
42+ return 2012 ;
43+ return 0 ;
44+ }
45+
3646static bool isMisraRuleActive (const std::set<std::string>& activeCheckers, const std::string& rule) {
3747 if (activeCheckers.count (" Misra C: " + rule))
3848 return true ;
@@ -229,29 +239,23 @@ std::string CheckersReport::getReport(const std::string& criticalErrors) const
229239 reportSection (" Cert C" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Cert C: " );
230240 reportSection (" Cert C++" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Cert C++: " );
231241
232- int misra = 0 ;
233- if (mSettings .premiumArgs .find (" misra-c-2012" ) != std::string::npos)
234- misra = 2012 ;
235- else if (mSettings .premiumArgs .find (" misra-c-2023" ) != std::string::npos)
236- misra = 2023 ;
237- else if (mSettings .addons .count (" misra" ))
238- misra = 2012 ;
242+ const int misraCVersion = getMisraCVersion (mSettings );
239243
240- if (misra == 0 ) {
244+ if (misraCVersion == 0 ) {
241245 fout << std::endl << std::endl;
242246 fout << " Misra C" << std::endl;
243247 fout << " -------" << std::endl;
244248 fout << " Misra is not enabled" << std::endl;
245249 } else {
246250 fout << std::endl << std::endl;
247- fout << " Misra C " << misra << std::endl;
251+ fout << " Misra C " << misraCVersion << std::endl;
248252 fout << " ------------" << std::endl;
249253 for (const checkers::MisraInfo& info: checkers::misraC2012Directives) {
250254 const std::string directive = " Dir " + std::to_string (info.a ) + " ." + std::to_string (info.b );
251255 const bool active = isMisraRuleActive (mActiveCheckers , directive);
252- fout << (active ? " Yes " : " No " ) << " Misra C " << misra << " : " << directive;
256+ fout << (active ? " Yes " : " No " ) << " Misra C " << misraCVersion << " : " << directive;
253257 std::string extra;
254- if (misra == 2012 && info.amendment >= 1 )
258+ if (misraCVersion == 2012 && info.amendment >= 1 )
255259 extra = " amendment:" + std::to_string (info.amendment );
256260 if (!extra.empty ())
257261 fout << std::string (10 - directive.size (), ' ' ) << extra;
@@ -260,9 +264,9 @@ std::string CheckersReport::getReport(const std::string& criticalErrors) const
260264 for (const checkers::MisraInfo& info: checkers::misraC2012Rules) {
261265 const std::string rule = std::to_string (info.a ) + " ." + std::to_string (info.b );
262266 const bool active = isMisraRuleActive (mActiveCheckers , rule);
263- fout << (active ? " Yes " : " No " ) << " Misra C " << misra << " : " << rule;
267+ fout << (active ? " Yes " : " No " ) << " Misra C " << misraCVersion << " : " << rule;
264268 std::string extra;
265- if (misra == 2012 && info.amendment >= 1 )
269+ if (misraCVersion == 2012 && info.amendment >= 1 )
266270 extra = " amendment:" + std::to_string (info.amendment );
267271 std::string reqs;
268272 if (info.amendment >= 3 )
@@ -284,84 +288,17 @@ std::string CheckersReport::getReport(const std::string& criticalErrors) const
284288std::string CheckersReport::getXmlReport (const std::string& criticalErrors) const
285289{
286290 std::string ret;
287-
288- if (!criticalErrors.empty ()) {
289- ret += " <critical-errors>" + criticalErrors + " \n </critical-errors>\n " ;
290- } else
291+ if (!criticalErrors.empty ())
292+ ret += " <critical-errors>" + criticalErrors + " </critical-errors>\n " ;
293+ else
291294 ret += " <critical-errors/>\n " ;
292295 ret += " <checkers-report>\n " ;
293-
294- const bool cppcheckPremium = isCppcheckPremium (mSettings );
295-
296- auto reportSection = [&ret, cppcheckPremium]
297- (const std::string& title,
298- const Settings& settings,
299- const std::set<std::string>& activeCheckers,
300- const std::map<std::string, std::string>& premiumCheckers,
301- const std::string& substring) {
302- if (!cppcheckPremium) {
303- ret += " <" + title + " />\n " ;
304- return ;
305- }
306- ret += " <" + title + " >\n " ;
307- for (const auto & checkReq: premiumCheckers) {
308- const std::string& checker = checkReq.first ;
309- if (checker.find (substring) == std::string::npos)
310- continue ;
311- bool active = cppcheckPremium && activeCheckers.count (checker) > 0 ;
312- if (substring == " ::" ) {
313- if (checkReq.second == " warning" )
314- active &= settings.severity .isEnabled (Severity::warning);
315- else if (checkReq.second == " style" )
316- active &= settings.severity .isEnabled (Severity::style);
317- else if (checkReq.second == " portability" )
318- active &= settings.severity .isEnabled (Severity::portability);
319- else if (!checkReq.second .empty ())
320- active = false ; // FIXME: handle req
321- }
322- ret += " <checker active=\" " + std::string (active ? " Yes" : " No" ) + " \" id=\" " + checker + " \" " ;
323- ret += " />\n " ;
324- }
325- ret += " </" + title + " >\n " ;
326- };
327-
328- reportSection (" premium-checkers" , mSettings , mActiveCheckers , checkers::premiumCheckers, " ::" );
329- reportSection (" autosar" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Autosar: " );
330- reportSection (" cert-c" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Cert C: " );
331- reportSection (" cert-cpp" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Cert C++: " );
332-
333- int misra = 0 ;
334- if (mSettings .premiumArgs .find (" misra-c-2012" ) != std::string::npos)
335- misra = 2012 ;
336- else if (mSettings .premiumArgs .find (" misra-c-2023" ) != std::string::npos)
337- misra = 2023 ;
338- else if (mSettings .addons .count (" misra" ))
339- misra = 2012 ;
340-
341- if (misra == 0 ) {
342- ret += " <misra-c/>\n " ;
343- } else {
344- ret += " <misra-c-" + std::to_string (misra) + " >\n " ;
345- for (const checkers::MisraInfo& info: checkers::misraC2012Directives) {
346- const std::string directive = " Dir " + std::to_string (info.a ) + " ." + std::to_string (info.b );
347- const bool active = isMisraRuleActive (mActiveCheckers , directive);
348- ret += " <checker active=\" " ;
349- ret += std::string (active ? " Yes" : " No" ) + " \" id=\" Misra C " + std::to_string (misra) + " : " + directive + " \" " ;
350- ret += " />\n " ;
351- }
352- for (const checkers::MisraInfo& info: checkers::misraC2012Rules) {
353- const std::string rule = std::to_string (info.a ) + " ." + std::to_string (info.b );
354- const bool active = isMisraRuleActive (mActiveCheckers , rule);
355- ret += " <checker active=\" " ;
356- ret += std::string (active ? " Yes" : " No" ) + " \" id=\" Misra C " + std::to_string (misra) + " : " + rule + " \" " ;
357- ret += " />\n " ;
358- }
359- ret += " </misra-c-" + std::to_string (misra) + " >\n " ;
296+ const int misraCVersion = getMisraCVersion (mSettings );
297+ for (std::string checker: mActiveCheckers ) {
298+ if (checker.compare (0 ,8 ," Misra C:" ) == 0 )
299+ checker = " Misra C " + std::to_string (misraCVersion) + " :" + checker.substr (8 );
300+ ret += " <checker id=\" " + checker + " \" />\n " ;
360301 }
361-
362- reportSection (" misra-cpp-2008" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Misra C++ 2008: " );
363- reportSection (" misra-cpp-2023" , mSettings , mActiveCheckers , checkers::premiumCheckers, " Misra C++ 2023: " );
364-
365302 ret += " </checkers-report>" ;
366303 return ret;
367304}
0 commit comments