Converter.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 FileHandle;
  22. use Data::Dumper;
  23. #
  24. # Export section
  25. #
  26. require Exporter;
  27. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $PREFERRED_PARSER);
  28. @ISA = qw(Exporter);
  29. @EXPORT = qw(swiConvert);
  30. @EXPORT_OK = qw();
  31. $VERSION = '1.0';
  32. $PREFERRED_PARSER = undef;
  33. #
  34. # Global variables
  35. #
  36. my $config = undef;
  37. #
  38. # Enter point
  39. #
  40. sub swiConvert
  41. {
  42. $config = shift();
  43. my $report = undef;
  44. my $exitCode = 0;
  45. $report = XMLin(
  46. $config->{"swi:report"}->{"swi:destination"} . "/"
  47. . $config->{"swi:report"}->{"swi:xml"}->{"swi:name"},
  48. ForceArray =>
  49. [ "swi:module", "swi:file", "swi:function", "swi:reference" ]
  50. );
  51. # generate notification report
  52. my $fh = new FileHandle(
  53. $config->{"swi:report"}->{"swi:destination"} . "/"
  54. . $config->{"swi:report"}->{"swi:notifications"}->{"swi:name"},
  55. "w"
  56. )
  57. or die("Can not open output file!");
  58. my $projectStat = $report->{"swi:statistic"};
  59. my $projectName = $config->{"swi:info"}->{"swi:project"}->{"swi:name"};
  60. my $projectLocation = $config->{"swi:report"}->{"swi:destination"};
  61. my $projectDiff = "modified";
  62. $exitCode +=
  63. swiNotificationPrint( $fh, $projectName, $projectLocation, undef,
  64. $projectStat, $projectDiff );
  65. for (
  66. my $moduleId = 0 ;
  67. $moduleId <= $#{ $report->{"swi:module"} } ;
  68. $moduleId++
  69. )
  70. {
  71. my $moduleStat = $report->{"swi:module"}[$moduleId]->{"swi:statistic"};
  72. my $moduleName = $report->{"swi:module"}[$moduleId]->{"swi:name"};
  73. my $moduleLocation =
  74. $report->{"swi:module"}[$moduleId]->{"swi:location"};
  75. my $moduleDiff =
  76. $report->{"swi:module"}[$moduleId]->{"swi:modification"};
  77. $exitCode +=
  78. swiNotificationPrint( $fh, $projectName . "/" . $moduleName,
  79. $moduleLocation, undef, $moduleStat, $moduleDiff );
  80. for (
  81. my $fileId = 0 ;
  82. $fileId <= $#{ $report->{"swi:module"}[$moduleId]->{"swi:file"} } ;
  83. $fileId++
  84. )
  85. {
  86. my $fileStat =
  87. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  88. ->{"swi:statistic"};
  89. my $fileName =
  90. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  91. ->{"swi:name"};
  92. my $fileLocation =
  93. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  94. ->{"swi:location"};
  95. my $fileDiff =
  96. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  97. ->{"swi:modification"};
  98. $exitCode += swiNotificationPrint(
  99. $fh, $projectName . "/" . $moduleName . "/" . $fileName,
  100. $moduleLocation, $fileLocation . ":0",
  101. $fileStat, $fileDiff
  102. );
  103. for (
  104. my $functionId = 0 ;
  105. $functionId <= $#{
  106. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  107. ->{"swi:function"}
  108. } ;
  109. $functionId++
  110. )
  111. {
  112. my $functionRefs =
  113. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  114. ->{"swi:function"}[$functionId]->{"swi:reference"};
  115. my $functionStat =
  116. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  117. ->{"swi:function"}[$functionId]->{"swi:statistic"};
  118. my $functionName =
  119. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  120. ->{"swi:function"}[$functionId]->{"swi:name"};
  121. my $functionLocation =
  122. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  123. ->{"swi:function"}[$functionId]->{"swi:location"};
  124. my $functionDiff =
  125. $report->{"swi:module"}[$moduleId]->{"swi:file"}[$fileId]
  126. ->{"swi:function"}[$functionId]->{"swi:modification"};
  127. $exitCode += swiNotificationPrint(
  128. $fh,
  129. $projectName . "/"
  130. . $moduleName . "/"
  131. . $fileName . "/"
  132. . $functionName,
  133. $moduleLocation,
  134. $functionLocation,
  135. $functionStat,
  136. $functionDiff,
  137. $functionRefs
  138. );
  139. }
  140. }
  141. }
  142. $fh->close();
  143. $fh = new FileHandle(
  144. $config->{"swi:report"}->{"swi:destination"} . "/"
  145. . $config->{"swi:report"}->{"swi:notifications"}->{"swi:name"},
  146. "r"
  147. )
  148. or die("Can not open input file!");
  149. while (<$fh>)
  150. {
  151. print STDERR $_;
  152. }
  153. $fh->close();
  154. return $exitCode;
  155. }
  156. sub swiNotificationPrint
  157. {
  158. my $file = shift();
  159. my $objName = shift();
  160. my $modLocation = shift();
  161. my $fileLocation = shift();
  162. my $objStat = shift();
  163. my $objDiff = shift();
  164. my $objRefs = shift();
  165. my $returnCode = 0;
  166. if ( !defined($fileLocation) )
  167. {
  168. $fileLocation = ".";
  169. }
  170. # Print 'swi:modifications'
  171. if ( $objDiff ne "unmodified"
  172. && $config->{"swi:report"}->{"swi:notifications"}->{"swi:print"}
  173. ->{ "swi:" . $objDiff }->{"swi:modifications"} eq "on" )
  174. {
  175. my $notification =
  176. "$modLocation/$fileLocation: " . "info"
  177. . ": Object "
  178. . $objName
  179. . " has been "
  180. . $objDiff
  181. . "\n\tObject : "
  182. . $objName . "\n";
  183. print $file $notification;
  184. print $file "\n";
  185. }
  186. # Print 'swi:failures'
  187. my $areThereDupViolations = 0;
  188. foreach my $keyStat ( keys %$objStat )
  189. {
  190. my $subStat = $objStat->{$keyStat};
  191. foreach my $keySubStat ( keys %$subStat )
  192. {
  193. my $types = $objStat->{$keyStat}->{$keySubStat};
  194. foreach my $type ( keys %$types )
  195. {
  196. my $statInfo = $objStat->{$keyStat}->{$keySubStat}->{$type};
  197. if (
  198. !(
  199. $statInfo->{"swi:level"} eq $statInfo->{"swi:suppress"}
  200. || ( $statInfo->{"swi:level"} eq "regular"
  201. && $statInfo->{"swi:suppress"} eq "undefined" )
  202. )
  203. )
  204. {
  205. my $notification =
  206. "$modLocation/$fileLocation: "
  207. . $statInfo->{"swi:level"}
  208. . ": Index '"
  209. . "$keyStat/$keySubStat/$type"
  210. . "' exceeds the limit"
  211. . "\n\tObject : "
  212. . $objName
  213. . "\n\tIndex value : "
  214. . $statInfo->{"content"}
  215. . "\n\tModification : "
  216. . $objDiff . " / "
  217. . $statInfo->{"swi:change"}
  218. . "\n\tSeverity : "
  219. . $statInfo->{"swi:level"}
  220. . "\n\tCriteria : "
  221. . $statInfo->{"swi:criteria"}
  222. . "\n\tSuppress level : "
  223. . $statInfo->{"swi:suppress"} . "\n";
  224. if ( $config->{"swi:report"}->{"swi:notifications"}
  225. ->{"swi:print"}->{ "swi:" . $objDiff }->{"swi:failures"}
  226. eq "on" )
  227. {
  228. print $file $notification;
  229. print $file "\n";
  230. if ( $keyStat eq "swi:duplication"
  231. && $keySubStat eq "swi:symbols"
  232. && $type eq 'swi:exact' )
  233. {
  234. $areThereDupViolations = 1;
  235. }
  236. }
  237. if ( $config->{"swi:report"}->{"swi:notifications"}
  238. ->{"swi:error"}->{ "swi:" . $objDiff } eq "on" )
  239. {
  240. $returnCode++;
  241. }
  242. }
  243. if ( $statInfo->{"swi:level"} eq "unresolved"
  244. || $statInfo->{"swi:suppress"} eq "unresolved" )
  245. {
  246. my $notification =
  247. "$modLocation/$fileLocation: "
  248. . $statInfo->{"swi:level"}
  249. . ": The level/severity of index '"
  250. . "$keyStat/$keySubStat/$type"
  251. . "' is unresolved"
  252. . "\n\tObject : "
  253. . $objName
  254. . "\n\tIndex value : "
  255. . $statInfo->{"content"}
  256. . "\n\tModification : "
  257. . $objDiff . " / "
  258. . $statInfo->{"swi:change"}
  259. . "\n\tSeverity : "
  260. . $statInfo->{"swi:level"}
  261. . "\n\tCriteria : "
  262. . $statInfo->{"swi:criteria"}
  263. . "\n\tSuppress level : "
  264. . $statInfo->{"swi:suppress"} . "\n\n";
  265. print $file $notification;
  266. $returnCode++;
  267. }
  268. }
  269. }
  270. }
  271. # Print 'swi:duplications'
  272. if (
  273. $areThereDupViolations == 1
  274. || $config->{"swi:report"}->{"swi:notifications"}->{"swi:print"}
  275. ->{ "swi:" . $objDiff }->{"swi:duplications"} eq "on"
  276. )
  277. {
  278. my $isPrinted = 0;
  279. foreach my $dupData ( @{$objRefs} )
  280. {
  281. if ( $dupData->{'swi:ref:type'} eq 'dup' )
  282. {
  283. print $file $modLocation . "/"
  284. . $dupData->{'swi:dup:file'} . ":"
  285. . $dupData->{'swi:dup:line'}
  286. . ": warning: '"
  287. . $dupData->{'swi:dup:size'}
  288. . "' executable symbols are duplicated in '"
  289. . $dupData->{'swi:dup:function'}
  290. . "' function\n";
  291. $isPrinted = 1;
  292. }
  293. }
  294. if ($isPrinted == 1)
  295. {
  296. print $file "\n";
  297. }
  298. }
  299. # Print 'swi:scanmessages'
  300. if ( $config->{"swi:report"}->{"swi:notifications"}->{"swi:print"}
  301. ->{ "swi:" . $objDiff }->{"swi:scanmessages"} eq "on" )
  302. {
  303. foreach my $scanData ( @{$objRefs} )
  304. {
  305. if (
  306. $scanData->{'swi:ref:type'} eq 'scan'
  307. && !(
  308. defined( $scanData->{'swi:scan:suppress'} )
  309. && $scanData->{'swi:scan:suppress'} eq 'on'
  310. )
  311. )
  312. {
  313. print $file $modLocation . "/"
  314. . $scanData->{'swi:scan:file'} . ":"
  315. . $scanData->{'swi:scan:line'}
  316. . ": warning: "
  317. . $scanData->{'swi:scan:message'}
  318. . "\n\tObject : "
  319. . $objName
  320. . "\n\tModification : "
  321. . $objDiff . "\n\n";
  322. $returnCode++;
  323. }
  324. }
  325. }
  326. return $returnCode;
  327. }
  328. return 1;