Converter.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. foreach my $keyStat ( keys %$objStat )
  188. {
  189. my $subStat = $objStat->{$keyStat};
  190. foreach my $keySubStat ( keys %$subStat )
  191. {
  192. my $types = $objStat->{$keyStat}->{$keySubStat};
  193. foreach my $type ( keys %$types )
  194. {
  195. my $statInfo = $objStat->{$keyStat}->{$keySubStat}->{$type};
  196. if (
  197. !(
  198. $statInfo->{"swi:level"} eq $statInfo->{"swi:suppress"}
  199. || ( $statInfo->{"swi:level"} eq "regular"
  200. && $statInfo->{"swi:suppress"} eq "undefined" )
  201. )
  202. )
  203. {
  204. my $notification =
  205. "$modLocation/$fileLocation: "
  206. . $statInfo->{"swi:level"}
  207. . ": Index '"
  208. . "$keyStat/$keySubStat/$type"
  209. . "' exceeds the limit"
  210. . "\n\tObject : "
  211. . $objName
  212. . "\n\tIndex value : "
  213. . $statInfo->{"content"}
  214. . "\n\tModification : "
  215. . $objDiff . " / "
  216. . $statInfo->{"swi:change"}
  217. . "\n\tSeverity : "
  218. . $statInfo->{"swi:level"}
  219. . "\n\tCriteria : "
  220. . $statInfo->{"swi:criteria"}
  221. . "\n\tSuppress level : "
  222. . $statInfo->{"swi:suppress"} . "\n";
  223. if ( $config->{"swi:report"}->{"swi:notifications"}
  224. ->{"swi:print"}->{ "swi:" . $objDiff }->{"swi:failures"}
  225. eq "on" )
  226. {
  227. print $file $notification;
  228. # Print 'swi:duplications'
  229. if ( $keyStat eq "swi:duplication"
  230. && $keySubStat eq "swi:symbols"
  231. && $config->{"swi:report"}->{"swi:notifications"}
  232. ->{"swi:print"}->{ "swi:" . $objDiff }
  233. ->{"swi:duplications"} eq "on" )
  234. {
  235. die('Internal Error occured!')
  236. if not defined($objRefs);
  237. print $file "\n";
  238. foreach my $dupData ( @{$objRefs} )
  239. {
  240. if ( $dupData->{'swi:ref:type'} eq 'dup' )
  241. {
  242. print $file $modLocation . "/"
  243. . $dupData->{'swi:dup:file'} . ":"
  244. . $dupData->{'swi:dup:line'}
  245. . ": warning: '"
  246. . $dupData->{'swi:dup:size'}
  247. . "' executable symbols are duplicated in '"
  248. . $dupData->{'swi:dup:function'}
  249. . "' function\n";
  250. }
  251. }
  252. }
  253. print $file "\n";
  254. }
  255. if ( $config->{"swi:report"}->{"swi:notifications"}
  256. ->{"swi:error"}->{ "swi:" . $objDiff } eq "on" )
  257. {
  258. $returnCode++;
  259. }
  260. }
  261. if ( $statInfo->{"swi:level"} eq "unresolved"
  262. || $statInfo->{"swi:suppress"} eq "unresolved" )
  263. {
  264. my $notification =
  265. "$modLocation/$fileLocation: "
  266. . $statInfo->{"swi:level"}
  267. . ": The level/severity of index '"
  268. . "$keyStat/$keySubStat/$type"
  269. . "' is unresolved"
  270. . "\n\tObject : "
  271. . $objName
  272. . "\n\tIndex value : "
  273. . $statInfo->{"content"}
  274. . "\n\tModification : "
  275. . $objDiff . " / "
  276. . $statInfo->{"swi:change"}
  277. . "\n\tSeverity : "
  278. . $statInfo->{"swi:level"}
  279. . "\n\tCriteria : "
  280. . $statInfo->{"swi:criteria"}
  281. . "\n\tSuppress level : "
  282. . $statInfo->{"swi:suppress"} . "\n\n";
  283. print $file $notification;
  284. $returnCode++;
  285. }
  286. }
  287. }
  288. }
  289. # Print 'swi:scanmessages'
  290. if ( $config->{"swi:report"}->{"swi:notifications"}->{"swi:print"}
  291. ->{ "swi:" . $objDiff }->{"swi:scanmessages"} eq "on" )
  292. {
  293. foreach my $scanData ( @{$objRefs} )
  294. {
  295. if (
  296. $scanData->{'swi:ref:type'} eq 'scan'
  297. && !(
  298. defined( $scanData->{'swi:scan:suppress'} )
  299. && $scanData->{'swi:scan:suppress'} eq 'on'
  300. )
  301. )
  302. {
  303. print $file $modLocation . "/"
  304. . $scanData->{'swi:scan:file'} . ":"
  305. . $scanData->{'swi:scan:line'}
  306. . ": warning: "
  307. . $scanData->{'swi:scan:message'}
  308. . "\n\tObject : "
  309. . $objName
  310. . "\n\tModification : "
  311. . $objDiff . "\n\n";
  312. $returnCode++;
  313. }
  314. }
  315. }
  316. return $returnCode;
  317. }
  318. return 1;