Converter.pm 12 KB

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