Appraiser.pm 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. #
  2. # Software Index, Copyright 2010, Software Index Project Team
  3. # Link: http://swi.sourceforge.net
  4. #
  5. # This file is part of Software Index Tool.
  6. #
  7. # Software Index is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Software Index 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 Software Index. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. use strict;
  20. use XML::Simple;
  21. use Internal::Output;
  22. use FileHandle;
  23. use Data::Dumper;
  24. #
  25. # Export section
  26. #
  27. require Exporter;
  28. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $PREFERRED_PARSER);
  29. @ISA = qw(Exporter);
  30. @EXPORT = qw(swiAppraise);
  31. @EXPORT_OK = qw();
  32. $VERSION = '1.0';
  33. $PREFERRED_PARSER = undef;
  34. #
  35. # Subroutine for troubleshooting purposes
  36. #
  37. use Internal::Output;
  38. #
  39. # Global variables
  40. #
  41. my $config = undef;
  42. my $report = undef;
  43. #
  44. # Enter point
  45. #
  46. sub swiAppraise
  47. {
  48. $config = shift();
  49. my $reportBase = undef;
  50. $report = XMLin(
  51. $config->{"swi:report"}->{"swi:destination"} . "/"
  52. . $config->{"swi:report"}->{"swi:xml"}->{"swi:name"} . ".x",
  53. ForceArray =>
  54. [ "swi:module", "swi:file", "swi:function", "swi:reference" ]
  55. );
  56. if ( defined( $config->{"swi:report"}->{"swi:xml"}->{"swi:baseline"} )
  57. && $config->{"swi:report"}->{"swi:xml"}->{"swi:baseline"} ne "" )
  58. {
  59. $reportBase = XMLin(
  60. $config->{"swi:report"}->{"swi:destination"} . "/"
  61. . $config->{"swi:report"}->{"swi:xml"}->{"swi:baseline"},
  62. ForceArray =>
  63. [ "swi:module", "swi:file", "swi:function", "swi:reference" ]
  64. );
  65. }
  66. my $projectStat = $report->{"swi:statistic"};
  67. for (
  68. my $moduleId = 0 ;
  69. $moduleId <= $#{ $report->{"swi:module"} } ;
  70. $moduleId++
  71. )
  72. {
  73. my $moduleStat = $report->{"swi:module"}[$moduleId]->{"swi:statistic"};
  74. for (
  75. my $fileId = 0 ;
  76. $fileId <= $#{ $report->{"swi:module"}[$moduleId]->{"swi:file"} } ;
  77. $fileId++
  78. )
  79. {
  80. my $fileStat =
  81. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  82. ->{"swi:statistic"};
  83. for (
  84. my $functionId = 0 ;
  85. $functionId <= $#{
  86. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  87. ->{"swi:function"}
  88. } ;
  89. $functionId++
  90. )
  91. {
  92. my $functionStat =
  93. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  94. ->{"swi:function"}[$functionId]->{"swi:statistic"};
  95. foreach my $keyStat ( keys %$functionStat )
  96. {
  97. my $subStat = $functionStat->{$keyStat};
  98. foreach my $keySubStat ( keys %$subStat )
  99. {
  100. # add total per file
  101. $fileStat->{$keyStat}->{$keySubStat}->{"swi:total"} +=
  102. $subStat->{$keySubStat}->{'swi:exact'};
  103. $fileStat->{$keyStat}->{$keySubStat}->{"swi:average"} =
  104. $fileStat->{$keyStat}->{$keySubStat}->{"swi:total"} /
  105. $fileStat->{"swi:count"}->{"swi:functions"}
  106. ->{'swi:exact'};
  107. # add total per module
  108. $moduleStat->{$keyStat}->{$keySubStat}->{"swi:total"} +=
  109. $subStat->{$keySubStat}->{'swi:exact'};
  110. $moduleStat->{$keyStat}->{$keySubStat}
  111. ->{"swi:average"} =
  112. $moduleStat->{$keyStat}->{$keySubStat}
  113. ->{"swi:total"} /
  114. $moduleStat->{"swi:count"}->{"swi:functions"}
  115. ->{'swi:exact'};
  116. # add total per project
  117. $projectStat->{$keyStat}->{$keySubStat}
  118. ->{"swi:total"} +=
  119. $subStat->{$keySubStat}->{'swi:exact'};
  120. $projectStat->{$keyStat}->{$keySubStat}
  121. ->{"swi:average"} =
  122. $projectStat->{$keyStat}->{$keySubStat}
  123. ->{"swi:total"} /
  124. $projectStat->{"swi:count"}->{"swi:functions"}
  125. ->{'swi:exact'};
  126. # add minimum per file
  127. if (
  128. !defined(
  129. $fileStat->{$keyStat}->{$keySubStat}
  130. ->{"swi:min"}
  131. )
  132. || $fileStat->{$keyStat}->{$keySubStat}
  133. ->{"swi:min"} >
  134. $subStat->{$keySubStat}->{'swi:exact'}
  135. )
  136. {
  137. $fileStat->{$keyStat}->{$keySubStat}->{"swi:min"} =
  138. $subStat->{$keySubStat}->{'swi:exact'};
  139. }
  140. # add minimum per module
  141. if (
  142. !defined(
  143. $moduleStat->{$keyStat}->{$keySubStat}
  144. ->{"swi:min"}
  145. )
  146. || $moduleStat->{$keyStat}->{$keySubStat}
  147. ->{"swi:min"} >
  148. $subStat->{$keySubStat}->{'swi:exact'}
  149. )
  150. {
  151. $moduleStat->{$keyStat}->{$keySubStat}
  152. ->{"swi:min"} =
  153. $subStat->{$keySubStat}->{'swi:exact'};
  154. }
  155. # add minimum per project
  156. if (
  157. !defined(
  158. $projectStat->{$keyStat}->{$keySubStat}
  159. ->{"swi:min"}
  160. )
  161. || $projectStat->{$keyStat}->{$keySubStat}
  162. ->{"swi:min"} >
  163. $subStat->{$keySubStat}->{'swi:exact'}
  164. )
  165. {
  166. $projectStat->{$keyStat}->{$keySubStat}
  167. ->{"swi:min"} =
  168. $subStat->{$keySubStat}->{'swi:exact'};
  169. }
  170. # add maximum per file
  171. if (
  172. !defined(
  173. $fileStat->{$keyStat}->{$keySubStat}
  174. ->{"swi:max"}
  175. )
  176. || $fileStat->{$keyStat}->{$keySubStat}
  177. ->{"swi:max"} <
  178. $subStat->{$keySubStat}->{'swi:exact'}
  179. )
  180. {
  181. $fileStat->{$keyStat}->{$keySubStat}->{"swi:max"} =
  182. $subStat->{$keySubStat}->{'swi:exact'};
  183. }
  184. # add maximum per module
  185. if (
  186. !defined(
  187. $moduleStat->{$keyStat}->{$keySubStat}
  188. ->{"swi:max"}
  189. )
  190. || $moduleStat->{$keyStat}->{$keySubStat}
  191. ->{"swi:max"} <
  192. $subStat->{$keySubStat}->{'swi:exact'}
  193. )
  194. {
  195. $moduleStat->{$keyStat}->{$keySubStat}
  196. ->{"swi:max"} =
  197. $subStat->{$keySubStat}->{'swi:exact'};
  198. }
  199. # add maximum per project
  200. if (
  201. !defined(
  202. $projectStat->{$keyStat}->{$keySubStat}
  203. ->{"swi:max"}
  204. )
  205. || $projectStat->{$keyStat}->{$keySubStat}
  206. ->{"swi:max"} <
  207. $subStat->{$keySubStat}->{'swi:exact'}
  208. )
  209. {
  210. $projectStat->{$keyStat}->{$keySubStat}
  211. ->{"swi:max"} =
  212. $subStat->{$keySubStat}->{'swi:exact'};
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. # generate full XML report
  220. my $outputFile =
  221. $config->{"swi:report"}->{"swi:destination"} . "/"
  222. . $config->{"swi:report"}->{"swi:xml"}->{"swi:name"};
  223. my $fh = new FileHandle( $outputFile, "w" )
  224. or die("Can not open output file '$outputFile'!");
  225. print $fh "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  226. print $fh "<swi:report>\n";
  227. print $fh "\n";
  228. print $fh " <swi:info>\n";
  229. print $fh " <swi:version>1.0</swi:version>\n";
  230. if ( defined( $ENV{USER} ) )
  231. {
  232. print $fh " <swi:user>" . $ENV{USER} . "</swi:user>\n";
  233. }
  234. print $fh " <swi:generator>SWI/APPRAISER</swi:generator>\n";
  235. print $fh " </swi:info>\n";
  236. print $fh "\n";
  237. $projectStat = $report->{"swi:statistic"};
  238. my $projectName = $config->{"swi:info"}->{"swi:project"}->{"swi:name"};
  239. my $projectDiff =
  240. swiReportModificationGet( $reportBase, $report, "swi:total" );
  241. for (
  242. my $moduleId = 0 ;
  243. $moduleId <= $#{ $report->{"swi:module"} } ;
  244. $moduleId++
  245. )
  246. {
  247. my $moduleStat = $report->{"swi:module"}[$moduleId]->{"swi:statistic"};
  248. my $moduleName = $report->{"swi:module"}[$moduleId]->{"swi:name"};
  249. my $moduleBase =
  250. swiReportObjectFind( $reportBase->{"swi:module"}, $moduleName );
  251. my $moduleDiff =
  252. swiReportModificationGet( $moduleBase,
  253. $report->{"swi:module"}[$moduleId], "swi:total" );
  254. print $fh " <swi:module>\n";
  255. print $fh " <swi:name>" . $moduleName . "</swi:name>\n";
  256. print $fh " <swi:location>"
  257. . $report->{"swi:module"}[$moduleId]->{"swi:location"}
  258. . "</swi:location>\n";
  259. print $fh " <swi:modification>"
  260. . $moduleDiff
  261. . "</swi:modification>\n";
  262. print $fh "\n";
  263. for (
  264. my $fileId = 0 ;
  265. $fileId <= $#{ $report->{"swi:module"}[$moduleId]->{"swi:file"} } ;
  266. $fileId++
  267. )
  268. {
  269. my $fileStat =
  270. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  271. ->{"swi:statistic"};
  272. my $fileName =
  273. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  274. ->{"swi:name"};
  275. my $fileBase =
  276. ( $moduleDiff eq "added" )
  277. ? undef
  278. : swiReportObjectFind( $moduleBase->{"swi:file"}, $fileName );
  279. my $fileDiff =
  280. swiReportModificationGet( $fileBase,
  281. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId],
  282. "swi:total" );
  283. print $fh " <swi:file>\n";
  284. print $fh " <swi:name>" . $fileName . "</swi:name>\n";
  285. print $fh " <swi:location>"
  286. . $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  287. ->{"swi:location"} . "</swi:location>\n";
  288. print $fh " <swi:modification>"
  289. . $fileDiff
  290. . "</swi:modification>\n";
  291. print $fh "\n";
  292. for (
  293. my $functionId = 0 ;
  294. $functionId <= $#{
  295. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  296. ->{"swi:function"}
  297. } ;
  298. $functionId++
  299. )
  300. {
  301. my $functionStat =
  302. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  303. ->{"swi:function"}[$functionId]->{"swi:statistic"};
  304. my $functionName =
  305. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  306. ->{"swi:function"}[$functionId]->{"swi:name"};
  307. my $functionBase =
  308. ( $fileDiff eq "added" )
  309. ? undef
  310. : swiReportObjectFind( $fileBase->{"swi:function"},
  311. $functionName );
  312. my $functionDiff = swiReportModificationGet(
  313. $functionBase,
  314. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  315. ->{"swi:function"}[$functionId],
  316. "swi:exact"
  317. );
  318. print $fh " <swi:function>\n";
  319. print $fh " "
  320. . XMLout( $functionName, RootName => 'swi:name' );
  321. print $fh " "
  322. . XMLout(
  323. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  324. ->{"swi:function"}[$functionId]->{"swi:location"},
  325. RootName => 'swi:location'
  326. );
  327. print $fh " "
  328. . XMLout(
  329. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  330. ->{"swi:function"}[$functionId]->{"swi:modifier"},
  331. RootName => 'swi:modifier'
  332. );
  333. print $fh " "
  334. . XMLout(
  335. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  336. ->{"swi:function"}[$functionId]->{"swi:pointer"},
  337. RootName => 'swi:pointer'
  338. );
  339. print $fh " <swi:modification>"
  340. . $functionDiff
  341. . "</swi:modification>\n";
  342. print $fh " <swi:statistic>\n";
  343. foreach my $keyStat ( keys %$functionStat )
  344. {
  345. print $fh " <" . $keyStat . ">\n";
  346. my $subStat = $functionStat->{$keyStat};
  347. foreach my $keySubStat ( keys %$subStat )
  348. {
  349. my ( $level, $suppress, $criteria ) =
  350. swiStatisticLevelGet(
  351. $keyStat,
  352. $keySubStat,
  353. "swi:exact",
  354. $projectName . "/"
  355. . $moduleName . "/"
  356. . $fileName . "/"
  357. . $functionName,
  358. $functionStat,
  359. "swi:function"
  360. );
  361. my $statDiff = swiStatisticDiffGet(
  362. $functionDiff,
  363. $functionStat->{$keyStat}->{$keySubStat}
  364. ->{'swi:exact'},
  365. $functionBase->{"swi:statistic"}->{$keyStat}
  366. ->{$keySubStat}->{"swi:exact"}->{'content'}
  367. );
  368. print $fh " <"
  369. . $keySubStat
  370. . "><swi:exact swi:change=\""
  371. . $statDiff
  372. . "\" swi:level=\""
  373. . $level
  374. . "\" swi:suppress=\""
  375. . $suppress
  376. . "\" swi:criteria=\""
  377. . $criteria . "\">"
  378. . $functionStat->{$keyStat}->{$keySubStat}
  379. ->{'swi:exact'}
  380. . "</swi:exact></"
  381. . $keySubStat . ">\n";
  382. }
  383. print $fh " </" . $keyStat . ">\n";
  384. }
  385. print $fh " </swi:statistic>\n";
  386. my $refers =
  387. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  388. ->{"swi:function"}[$functionId]->{'swi:reference'};
  389. if ( defined($refers) )
  390. {
  391. foreach my $refData ( @{$refers} )
  392. {
  393. if ( $refData->{'swi:ref:type'} eq 'scan' )
  394. {
  395. foreach my $pattern (
  396. @{
  397. $config->{'swi:modules'}
  398. ->{"swi:module"}[$moduleId]
  399. ->{'swi:scanner'}->{'swi:suppress'}
  400. ->{'swi:pattern'}
  401. }
  402. )
  403. {
  404. my $msgPattern = $pattern->{'swi:message'};
  405. my $objPattern = $pattern->{'content'};
  406. if ( $refData->{'swi:scan:message'} =~
  407. m/$msgPattern/
  408. && "$projectName/$moduleName/$fileName/$functionName"
  409. =~ m/$objPattern/ )
  410. {
  411. $refData->{'swi:scan:suppress'} = 'on';
  412. $pattern->{'swi:used'} = 1;
  413. last;
  414. }
  415. }
  416. }
  417. }
  418. my $refStr = XMLout( $refers, RootName => '' );
  419. $refStr =~ s/\n/\n /g;
  420. $refStr =~ s/<anon /<swi:reference /g;
  421. print $fh " ";
  422. print $fh $refStr;
  423. print $fh "\n";
  424. }
  425. print $fh " </swi:function>\n";
  426. print $fh "\n";
  427. }
  428. for (
  429. my $functionId = 0 ;
  430. $functionId <= $#{ $fileBase->{"swi:function"} } ;
  431. $functionId++
  432. )
  433. {
  434. my $functionOld = $fileBase->{"swi:function"}[$functionId];
  435. if (
  436. swiReportObjectFind(
  437. $report->{"swi:module"}[$moduleId]
  438. ->{"swi:file"}[$fileId]->{"swi:function"},
  439. $functionOld->{"swi:name"}
  440. ) == undef
  441. )
  442. {
  443. print $fh " <swi:function>\n";
  444. print $fh " <swi:name>"
  445. . $functionOld->{"swi:name"}
  446. . "</swi:name>\n";
  447. print $fh " <swi:location>"
  448. . $functionOld->{"swi:location"}
  449. . "</swi:location>\n";
  450. print $fh
  451. " <swi:modification>removed</swi:modification>\n";
  452. print $fh " </swi:function>\n";
  453. print $fh "\n";
  454. }
  455. }
  456. print $fh " <swi:statistic>\n";
  457. foreach my $keyStat ( keys %$fileStat )
  458. {
  459. print $fh " <" . $keyStat . ">\n";
  460. my $subStat = $fileStat->{$keyStat};
  461. foreach my $keySubStat ( keys %$subStat )
  462. {
  463. my @types = (
  464. "swi:exact", "swi:average",
  465. "swi:min", "swi:max",
  466. "swi:total"
  467. );
  468. print $fh " <" . $keySubStat . ">\n";
  469. foreach my $type (@types)
  470. {
  471. if (
  472. defined(
  473. $fileStat->{$keyStat}->{$keySubStat}->{$type}
  474. )
  475. )
  476. {
  477. my ( $level, $suppress, $criteria ) =
  478. swiStatisticLevelGet(
  479. $keyStat,
  480. $keySubStat,
  481. $type,
  482. $projectName . "/"
  483. . $moduleName . "/"
  484. . $fileName,
  485. $fileStat,
  486. "swi:file"
  487. );
  488. my $statDiff = swiStatisticDiffGet(
  489. $fileDiff,
  490. $fileStat->{$keyStat}->{$keySubStat}->{$type},
  491. $fileBase->{"swi:statistic"}->{$keyStat}
  492. ->{$keySubStat}->{$type}->{'content'}
  493. );
  494. print $fh " <" . $type
  495. . " swi:change=\""
  496. . $statDiff
  497. . "\" swi:level=\""
  498. . $level
  499. . "\" swi:suppress=\""
  500. . $suppress
  501. . "\" swi:criteria=\""
  502. . $criteria . "\">"
  503. . sprintf( "%.2f",
  504. $fileStat->{$keyStat}->{$keySubStat}->{$type} )
  505. . "</"
  506. . $type . ">\n";
  507. }
  508. }
  509. print $fh " </" . $keySubStat . ">\n";
  510. }
  511. print $fh " </" . $keyStat . ">\n";
  512. }
  513. print $fh " </swi:statistic>\n";
  514. print $fh " </swi:file>\n";
  515. print $fh "\n";
  516. }
  517. for (
  518. my $fileId = 0 ;
  519. $fileId <= $#{ $moduleBase->{"swi:file"} } ;
  520. $fileId++
  521. )
  522. {
  523. my $fileOld = $moduleBase->{"swi:file"}[$fileId];
  524. if (
  525. swiReportObjectFind(
  526. $report->{"swi:module"}[$moduleId]->{"swi:file"},
  527. $fileOld->{"swi:name"} ) == undef
  528. )
  529. {
  530. print $fh " <swi:file>\n";
  531. print $fh " <swi:name>"
  532. . $fileOld->{"swi:name"}
  533. . "</swi:name>\n";
  534. print $fh " <swi:location>"
  535. . $fileOld->{"swi:location"}
  536. . "</swi:location>\n";
  537. print $fh
  538. " <swi:modification>removed</swi:modification>\n";
  539. print $fh " </swi:file>\n";
  540. print $fh "\n";
  541. }
  542. }
  543. print $fh " <swi:statistic>\n";
  544. foreach my $keyStat ( keys %$moduleStat )
  545. {
  546. print $fh " <" . $keyStat . ">\n";
  547. my $subStat = $moduleStat->{$keyStat};
  548. foreach my $keySubStat ( keys %$subStat )
  549. {
  550. my @types = (
  551. "swi:exact", "swi:average", "swi:min", "swi:max",
  552. "swi:total"
  553. );
  554. print $fh " <" . $keySubStat . ">\n";
  555. foreach my $type (@types)
  556. {
  557. if (
  558. defined(
  559. $moduleStat->{$keyStat}->{$keySubStat}->{$type}
  560. )
  561. )
  562. {
  563. my ( $level, $suppress, $criteria ) =
  564. swiStatisticLevelGet( $keyStat, $keySubStat, $type,
  565. $projectName . "/" . $moduleName,
  566. $moduleStat, "swi:module" );
  567. my $statDiff = swiStatisticDiffGet(
  568. $moduleDiff,
  569. $moduleStat->{$keyStat}->{$keySubStat}->{$type},
  570. $moduleBase->{"swi:statistic"}->{$keyStat}
  571. ->{$keySubStat}->{$type}->{'content'}
  572. );
  573. print $fh " <" . $type
  574. . " swi:change=\""
  575. . $statDiff
  576. . "\" swi:level=\""
  577. . $level
  578. . "\" swi:suppress=\""
  579. . $suppress
  580. . "\" swi:criteria=\""
  581. . $criteria . "\">"
  582. . sprintf( "%.2f",
  583. $moduleStat->{$keyStat}->{$keySubStat}->{$type} )
  584. . "</"
  585. . $type . ">\n";
  586. }
  587. }
  588. print $fh " </" . $keySubStat . ">\n";
  589. }
  590. print $fh " </" . $keyStat . ">\n";
  591. }
  592. print $fh " </swi:statistic>\n";
  593. print $fh " </swi:module>\n";
  594. print $fh "\n";
  595. }
  596. for (
  597. my $moduleId = 0 ;
  598. $moduleId <= $#{ $reportBase->{"swi:module"} } ;
  599. $moduleId++
  600. )
  601. {
  602. my $moduleOld = $reportBase->{"swi:module"}[$moduleId];
  603. if (
  604. swiReportObjectFind( $report->{"swi:module"},
  605. $moduleOld->{"swi:name"} ) == undef
  606. )
  607. {
  608. print $fh " <swi:module>\n";
  609. print $fh " <swi:name>"
  610. . $moduleOld->{"swi:name"}
  611. . "</swi:name>\n";
  612. print $fh " <swi:location>"
  613. . $moduleOld->{"swi:location"}
  614. . "</swi:location>\n";
  615. print $fh " <swi:modification>removed</swi:modification>\n";
  616. print $fh " </swi:module>\n";
  617. print $fh "\n";
  618. }
  619. }
  620. print $fh " <swi:statistic>\n";
  621. foreach my $keyStat ( keys %$projectStat )
  622. {
  623. print $fh " <" . $keyStat . ">\n";
  624. my $subStat = $projectStat->{$keyStat};
  625. foreach my $keySubStat ( keys %$subStat )
  626. {
  627. my @types =
  628. ( "swi:exact", "swi:average", "swi:min", "swi:max", "swi:total" );
  629. print $fh " <" . $keySubStat . ">\n";
  630. foreach my $type (@types)
  631. {
  632. if (
  633. defined( $projectStat->{$keyStat}->{$keySubStat}->{$type} )
  634. )
  635. {
  636. my ( $level, $suppress, $criteria ) = swiStatisticLevelGet(
  637. $keyStat, $keySubStat, $type,
  638. $projectName, $projectStat, "swi:project"
  639. );
  640. my $statDiff = swiStatisticDiffGet(
  641. $projectDiff,
  642. $projectStat->{$keyStat}->{$keySubStat}->{$type},
  643. $reportBase->{"swi:statistic"}->{$keyStat}
  644. ->{$keySubStat}->{$type}->{'content'}
  645. );
  646. print $fh " <" . $type
  647. . " swi:change=\""
  648. . $statDiff
  649. . "\" swi:level=\""
  650. . $level
  651. . "\" swi:suppress=\""
  652. . $suppress
  653. . "\" swi:criteria=\""
  654. . $criteria . "\">"
  655. . sprintf( "%.2f",
  656. $projectStat->{$keyStat}->{$keySubStat}->{$type} )
  657. . "</"
  658. . $type . ">\n";
  659. }
  660. }
  661. print $fh " </" . $keySubStat . ">\n";
  662. }
  663. print $fh " </" . $keyStat . ">\n";
  664. }
  665. print $fh " </swi:statistic>\n";
  666. print $fh "</swi:report>\n";
  667. swiCheckUselessPatterns($config);
  668. return 0;
  669. }
  670. sub swiStatisticLevelGet
  671. {
  672. my $keyStat = shift();
  673. my $keySubStat = shift();
  674. my $type = shift();
  675. my $objName = shift();
  676. my $objStat = shift();
  677. my $objType = shift();
  678. my $statValue = undef;
  679. # Array of results: level, suppress level, criteria
  680. my @returnResult = ( "undefined", "undefined", "" );
  681. if (
  682. defined( $config->{"swi:limits"}->{$keyStat}->{$keySubStat}->{$type} ) )
  683. {
  684. my $limit = $config->{"swi:limits"}->{$keyStat}->{$keySubStat}->{$type};
  685. my $objectPattern = $limit->{"swi:objectpattern"};
  686. if ( defined($objectPattern) && $objName !~ m/$objectPattern/ )
  687. {
  688. $returnResult[2] = '[limit not applied]';
  689. }
  690. else
  691. {
  692. if ( defined( $limit->{"swi:relation"} ) )
  693. {
  694. my @relation = undef;
  695. @relation = split( /\//, $limit->{"swi:relation"} );
  696. my $factor =
  697. $objStat->{ $relation[0] }->{ $relation[1] }
  698. ->{ $relation[2] };
  699. if ( !defined($factor) )
  700. {
  701. STATUS(
  702. "Wrong configuration for the limit '$keyStat/$keySubStat/$type'. Relation "
  703. . $limit->{"swi:relation"}
  704. . " is not found for the object '$objName'"
  705. );
  706. $factor = 0;
  707. }
  708. if ($factor == 0)
  709. {
  710. # Devide zero by zero, equals to 1
  711. if ($objStat->{$keyStat}->{$keySubStat}->{$type} == 0)
  712. {
  713. $statValue = "1.00";
  714. }
  715. # Devide negative number by zero, equals to -infinity
  716. elsif ($objStat->{$keyStat}->{$keySubStat}->{$type} < 0)
  717. {
  718. $statValue = '(-)Infinity';
  719. }
  720. # Devide positive number by zero, equals to infinity
  721. else
  722. {
  723. $statValue = '(+)Infinity';
  724. }
  725. }
  726. else
  727. {
  728. $statValue = $objStat->{$keyStat}->{$keySubStat}->{$type} / $factor;
  729. $statValue = sprintf( "%.2f", $statValue );
  730. }
  731. }
  732. else
  733. {
  734. $statValue = sprintf( "%.2f", $objStat->{$keyStat}->{$keySubStat}->{$type} );
  735. }
  736. if ( $limit->{"swi:warning"} > $limit->{"swi:notice"}
  737. && $limit->{"swi:notice"} > $limit->{"swi:info"} )
  738. {
  739. if ( $statValue eq '(-)Infinity' )
  740. {
  741. $returnResult[0] = "regular";
  742. $returnResult[2] = "["
  743. . $statValue
  744. . " less than "
  745. . $limit->{"swi:info"} . "]";
  746. }
  747. elsif ( $statValue eq '(+)Infinity' || $statValue > $limit->{"swi:warning"} )
  748. {
  749. $returnResult[0] = "warning";
  750. $returnResult[2] = "["
  751. . $statValue
  752. . " greater than "
  753. . $limit->{"swi:warning"} . "]";
  754. }
  755. elsif ( $statValue > $limit->{"swi:notice"} )
  756. {
  757. $returnResult[0] = "notice";
  758. $returnResult[2] = "["
  759. . $statValue
  760. . " greater than "
  761. . $limit->{"swi:notice"} . "]";
  762. }
  763. elsif ( $statValue > $limit->{"swi:info"} )
  764. {
  765. $returnResult[0] = "info";
  766. $returnResult[2] = "["
  767. . $statValue
  768. . " greater than "
  769. . $limit->{"swi:info"} . "]";
  770. }
  771. else
  772. {
  773. $returnResult[0] = "regular";
  774. $returnResult[2] = "["
  775. . $statValue
  776. . " less than "
  777. . $limit->{"swi:info"} . "]";
  778. }
  779. }
  780. elsif ($limit->{"swi:warning"} < $limit->{"swi:notice"}
  781. && $limit->{"swi:notice"} < $limit->{"swi:info"} )
  782. {
  783. if ( $statValue eq '(+)Infinity' )
  784. {
  785. $returnResult[0] = "regular";
  786. $returnResult[2] = "["
  787. . $statValue
  788. . " greater than "
  789. . $limit->{"swi:info"} . "]";
  790. }
  791. elsif ( $statValue eq '(-)Infinity' || $statValue < $limit->{"swi:warning"} )
  792. {
  793. $returnResult[0] = "warning";
  794. $returnResult[2] = "["
  795. . $statValue
  796. . " less than "
  797. . $limit->{"swi:warning"} . "]";
  798. }
  799. elsif ( $statValue < $limit->{"swi:notice"} )
  800. {
  801. $returnResult[0] = "notice";
  802. $returnResult[2] = "["
  803. . $statValue
  804. . " less than "
  805. . $limit->{"swi:notice"} . "]";
  806. }
  807. elsif ( $statValue < $limit->{"swi:info"} )
  808. {
  809. $returnResult[0] = "info";
  810. $returnResult[2] = "["
  811. . $statValue
  812. . " less than "
  813. . $limit->{"swi:info"} . "]";
  814. }
  815. else
  816. {
  817. $returnResult[0] = "regular";
  818. $returnResult[2] = "["
  819. . $statValue
  820. . " greater than "
  821. . $limit->{"swi:info"} . "]";
  822. }
  823. }
  824. else
  825. {
  826. STATUS(
  827. "Wrong settings in configuration file (swi:limits section): swi:limit/$keyStat/$keySubStat/$type"
  828. );
  829. $returnResult[0] = "unresolved";
  830. }
  831. # check if suppressed
  832. my $isFound = 0;
  833. LOOPPATTERNS:
  834. foreach ( @{ $limit->{"swi:suppress"}->{"swi:pattern"} } )
  835. {
  836. my $pattern = $_;
  837. if ( ref($pattern) eq "HASH"
  838. && defined( $pattern->{"swi:level"} ) )
  839. {
  840. my $content = $pattern->{"content"};
  841. if ( $objName =~ m/$content/ )
  842. {
  843. if ( $isFound == 0 )
  844. {
  845. $returnResult[1] = $pattern->{"swi:level"};
  846. $pattern->{'swi:used'} = 1;
  847. $isFound = 1;
  848. }
  849. else
  850. {
  851. # This object is matched by several patterns
  852. if ( $returnResult[1] ne $pattern->{"swi:level"} )
  853. {
  854. # and levels are not equal in different patterns
  855. STATUS(
  856. "Configuration is wrong: $objName is matched by several patterns"
  857. );
  858. $returnResult[1] = "unresolved";
  859. }
  860. }
  861. }
  862. }
  863. else
  864. {
  865. STATUS(
  866. "Wrong settings in configuration file (swi:limits section): swi:limits/$keyStat/$keySubStat/$type: "
  867. . "Level is missed in pattern for the object '$objType'"
  868. );
  869. $returnResult[1] = "unresolved";
  870. $returnResult[2] = "[]";
  871. }
  872. }
  873. }
  874. }
  875. return @returnResult;
  876. }
  877. sub swiStatisticDiffGet
  878. {
  879. my $objDiff = shift();
  880. my $newStat = shift();
  881. my $oldStat = shift();
  882. if ( $objDiff ne "added" )
  883. {
  884. return sprintf( "%.2f", $newStat - $oldStat );
  885. }
  886. return "";
  887. }
  888. sub swiReportObjectFind
  889. {
  890. my $objects = shift();
  891. my $objName = shift();
  892. foreach (@$objects)
  893. {
  894. if ( $_->{"swi:name"} eq $objName
  895. && $_->{"swi:modification"} ne "removed" )
  896. {
  897. return $_;
  898. }
  899. }
  900. return undef;
  901. }
  902. sub swiReportModificationGet
  903. {
  904. my $objBase = shift();
  905. my $objNew = shift();
  906. my $statType = shift();
  907. if ( !defined($objBase) )
  908. {
  909. return "added";
  910. }
  911. my $newCrc =
  912. $objNew->{"swi:statistic"}->{"swi:checksum"}->{"swi:source"}->{$statType};
  913. my $newLength =
  914. $objNew->{"swi:statistic"}->{"swi:length"}->{"swi:source"}->{$statType};
  915. my $newDup =
  916. $objNew->{"swi:statistic"}->{"swi:duplication"}->{"swi:symbols"}
  917. ->{$statType};
  918. if ( $objBase->{"swi:statistic"}->{"swi:checksum"}->{"swi:source"}
  919. ->{$statType}->{'content'} != $newCrc
  920. || $objBase->{"swi:statistic"}->{"swi:length"}->{"swi:source"}
  921. ->{$statType}->{'content'} != $newLength )
  922. {
  923. return "modified";
  924. }
  925. if ( $objBase->{"swi:statistic"}->{"swi:duplication"}->{"swi:symbols"}
  926. ->{$statType}->{'content'} != $newDup )
  927. {
  928. return "cloned";
  929. }
  930. return "unmodified";
  931. }
  932. sub swiCheckUselessPatterns
  933. {
  934. my $root = shift();
  935. if ( ref($root) eq "HASH" )
  936. {
  937. foreach my $key ( keys %{$root} )
  938. {
  939. if ( $key eq 'swi:pattern' )
  940. {
  941. foreach my $pattern ( @{ $root->{'swi:pattern'} } )
  942. {
  943. if ( !defined( $pattern->{'swi:used'} )
  944. || $pattern->{'swi:used'} == 0 )
  945. {
  946. my $data = Dumper($pattern);
  947. $data =~ s/\n/ /g;
  948. $data =~ s/\s+/ /g;
  949. STATUS(
  950. "Useless suppress option detected with the following content: $data"
  951. );
  952. }
  953. }
  954. return;
  955. }
  956. swiCheckUselessPatterns( $root->{$key} );
  957. }
  958. }
  959. elsif ( ref($root) eq "ARRAY" )
  960. {
  961. foreach ( @{$root} )
  962. {
  963. return swiCheckUselessPatterns($_);
  964. }
  965. }
  966. return;
  967. }
  968. return 1;