Processor.pm 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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 Data::Dumper;
  21. use FileHandle;
  22. use IPC::Open3;
  23. use XML::Simple;
  24. use String::CRC::Cksum;
  25. #
  26. # Export section
  27. #
  28. require Exporter;
  29. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $PREFERRED_PARSER);
  30. @ISA = qw(Exporter);
  31. @EXPORT = qw(swiProcess);
  32. @EXPORT_OK = qw();
  33. $VERSION = '1.0';
  34. $PREFERRED_PARSER = undef;
  35. #
  36. # Subroutine for troubleshooting purposes
  37. #
  38. use Internal::Output;
  39. #
  40. # Global variables
  41. #
  42. my @crctab = (
  43. 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
  44. 0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
  45. 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
  46. 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
  47. 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
  48. 0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
  49. 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
  50. 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
  51. 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
  52. 0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
  53. 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
  54. 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
  55. 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
  56. 0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
  57. 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
  58. 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
  59. 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
  60. 0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
  61. 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
  62. 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
  63. 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
  64. 0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
  65. 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
  66. 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
  67. 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
  68. 0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
  69. 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
  70. 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
  71. 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
  72. 0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
  73. 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
  74. 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
  75. 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
  76. 0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
  77. 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
  78. 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
  79. 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
  80. 0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
  81. 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
  82. 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
  83. 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
  84. 0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
  85. 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
  86. );
  87. #
  88. # Brackets/hooks '()' in regexp are not acceptable in declarations below
  89. # TODO: configure individually for every language
  90. #
  91. my $regexpCommentSingle = '//';
  92. my $regexpCommentMultiStart = '/\\*';
  93. my $regexpCommentMultiEnd = '\\*/';
  94. my $regexpCodeBlockStart = '{';
  95. my $regexpCodeBlockEnd = '}';
  96. my $regexpCodeKeyword = 'case|do|else|if|for|switch|while';
  97. my $regexpCodeStatements =
  98. '\s*[^;]*[;]\s*|\s*extern\s+["]{2}\s*|\s*protected\s*[:]\s*|\s*private\s*[:]\s*|\s*public\s*[:]\s*';
  99. my $regexpCodeFunctionIdentifier =
  100. '(([_a-zA-Z:~][_a-zA-Z0-9:~]*[:][:])*operator[^a-zA-Z0-9_][^(]*)|([_a-zA-Z:~][_a-zA-Z0-9:~]*)';
  101. my $regexpCodeFunctionModifier = '[_a-zA-Z0-9:*&><, \t\n]*\s+[*&]?[*&]?\s*';
  102. my $regexpCodeFunctionArguments = '\s*\([^;]+\s*';
  103. my $regexpCodeContainerIdentifier = '[_a-zA-Z][_a-zA-Z0-9]*';
  104. my $regexpCodeContainerModifier =
  105. '\s*class|\s*template\s*<[^;]+>\s*class|\s*namespace|\s*template\s*<[^;]+>\s*struct|\s*struct|\s*static\s+struct|\s*static\s+union|\s*static\s+class|\s*enum|\s*typedef|\s*typedef\s+struct|\s*typedef\s+enum|\s*union|\s*typedef\s+union';
  106. my $regexpCodeContainerArguments = '\s*[^;]*\s*';
  107. my $regexpCodeContainerDelimeter = '::';
  108. my $regexpCodeBlockStartIgnore =
  109. '=\s*|,\s*|\s+enum\s*|\s+union\s*|\s+struct\s*';
  110. my $regexpCodeStringBorder_Escape =
  111. '(([^\\\](\\\\\\\\)*\\\\)|^([\\\](\\\\\\\\)*))';
  112. my $regexpCodeStringBorder_Text = '["\']';
  113. my $regexpCodePreprocessorStart = '^[ \t]*#';
  114. my $regexpCodePreprocessorEnd = '[^\\\\]\s*$';
  115. my $regexpCodeMatch_ComplexityCyclomatic =
  116. '([^0-9A-Za-z_]((if)|(case)|(for)|(while))[^0-9A-Za-z_])|[&][&]|[|][|]|[?]"';
  117. my $regexpCodeGlobalFunctionName = '>>>GLOBAL<<<';
  118. #
  119. # Handler for dupindex tool
  120. #
  121. my $dupindexHandler = undef;
  122. my $dupindexIn = undef;
  123. my $dupindexOut = undef;
  124. my $dupindexErr = undef;
  125. #
  126. # Errors counter, this global variable incremented if some inconsistency detected
  127. #
  128. my $exitCode = 0;
  129. #
  130. # Enter point
  131. #
  132. sub swiProcess
  133. {
  134. my $returnCode = 0;
  135. my $config = shift();
  136. my $moduleId = shift();
  137. my $rootLocation = shift();
  138. my $swiGlobalDirectory =
  139. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:location"};
  140. my $swiGlobalReportLocation =
  141. $config->{"swi:report"}->{"swi:destination"} . "/"
  142. . $config->{"swi:report"}->{"swi:xml"}->{"swi:name"}
  143. . ".$moduleId";
  144. my $swiGlobalModuleName =
  145. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:name"};
  146. my $swiGlobalInclude =
  147. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:files"}
  148. ->{"swi:include"};
  149. my $swiGlobalExclude =
  150. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:files"}
  151. ->{"swi:exclude"};
  152. my $swiGlobalReportName =
  153. $config->{"swi:info"}->{"swi:project"}->{"swi:name"};
  154. my $swiGlobalPreprocessorRules =
  155. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:preprocessor"}
  156. ->{"swi:rule"};
  157. my $swiGlobalScanerRules =
  158. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:scanner"}
  159. ->{"swi:rule"};
  160. my $swiGlobalDupfinderEnabled =
  161. $config->{"swi:modules"}->{"swi:module"}[$moduleId]->{"swi:indexer:dup"}
  162. ->{"swi:enabled"};
  163. if ( $returnCode == 0 )
  164. {
  165. if ( $swiGlobalDirectory eq "" )
  166. {
  167. STATUS(
  168. "Wrong configuration: source directory should be specified!");
  169. $returnCode = -1;
  170. }
  171. }
  172. if ( $returnCode == 0 )
  173. {
  174. if ( $swiGlobalReportLocation eq "" )
  175. {
  176. STATUS(
  177. "Wrong configuiration: report location should be specified!");
  178. $returnCode = -2;
  179. }
  180. }
  181. if ( not defined($swiGlobalInclude) )
  182. {
  183. $swiGlobalInclude = '.*';
  184. }
  185. if ( $returnCode == 0 )
  186. {
  187. my $filesData = {};
  188. $dupindexHandler =
  189. open3( $dupindexIn, $dupindexOut, $dupindexErr,
  190. "$rootLocation/dupindex/bin/dupindex.exe" );
  191. my $dupfinderSettings =
  192. $config->{"swi:modules"}->{"swi:module"}[$moduleId]
  193. ->{"swi:indexer:dup"};
  194. if ( defined( $dupfinderSettings->{"swi:minlength"} ) )
  195. {
  196. print $dupindexIn "init_length\n";
  197. print $dupindexIn $dupfinderSettings->{"swi:minlength"} . "\n";
  198. }
  199. if ( defined( $dupfinderSettings->{"swi:proximity"} ) )
  200. {
  201. print $dupindexIn "init_proximity\n";
  202. print $dupindexIn $dupfinderSettings->{"swi:proximity"} . "\n";
  203. }
  204. # TODO: configure individually for every language
  205. # see for todos in dupindex.cpp file
  206. #print $dupindexIn "init_ignorable\n";
  207. #print $dupindexIn "\n";
  208. #print $dupindexIn "init_nonregular\n";
  209. #print $dupindexIn "\n";
  210. opendir( DIR, $swiGlobalDirectory )
  211. or die("Can not open source directory '$swiGlobalDirectory'!");
  212. while ( my $file = readdir(DIR) )
  213. {
  214. if ( $file =~ m/$swiGlobalInclude/ )
  215. {
  216. if ($swiGlobalExclude ne ""
  217. && $file =~ m/$swiGlobalExclude/ )
  218. {
  219. next;
  220. }
  221. $filesData->{$file} = swiParse(
  222. $swiGlobalDirectory,
  223. $file,
  224. $swiGlobalPreprocessorRules,
  225. $swiGlobalScanerRules,
  226. $config->{"swi:modules"}->{"swi:module"}[$moduleId]
  227. ->{"swi:indexer:dup"}
  228. );
  229. }
  230. }
  231. closedir(DIR);
  232. # Add duplication statistics
  233. if ( defined($swiGlobalDupfinderEnabled)
  234. && $swiGlobalDupfinderEnabled eq 'on' )
  235. {
  236. $filesData = swiSourceIndexDuplicationAdd($filesData);
  237. }
  238. else
  239. {
  240. print $dupindexIn "exit\n";
  241. }
  242. # Prepare XML report
  243. STATUS("Generating the report '$swiGlobalReportLocation'...");
  244. swiXmlReportPrint(
  245. $filesData, $swiGlobalDirectory,
  246. $swiGlobalReportLocation, $swiGlobalModuleName
  247. );
  248. $returnCode = $exitCode;
  249. }
  250. return $returnCode;
  251. }
  252. sub swiXmlReportPrint()
  253. {
  254. my $filesData = shift();
  255. my $curDir = shift();
  256. my $reportLocation = shift();
  257. my $moduleName = shift();
  258. $curDir =~ s/\n$//;
  259. my $fh = new FileHandle( "$reportLocation", "w" )
  260. or die("Can not open output file: $reportLocation!");
  261. print $fh "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  262. print $fh "<swi:report>\n";
  263. print $fh "\n";
  264. print $fh " <swi:module>\n";
  265. print $fh " <swi:name>" . $moduleName . "</swi:name>\n";
  266. print $fh " <swi:location>" . $curDir . "</swi:location>\n";
  267. print $fh "\n";
  268. print $fh " <swi:info>\n";
  269. print $fh " <swi:version>1</swi:version>\n";
  270. if ( defined( $ENV{USER} ) )
  271. {
  272. print $fh " <swi:user>" . $ENV{USER} . "</swi:user>\n";
  273. }
  274. print $fh " <swi:generator>SWI/PROCESSOR</swi:generator>\n";
  275. print $fh " </swi:info>\n";
  276. print $fh "\n";
  277. my $filesCount = 0;
  278. my $functionsCount = 0;
  279. foreach my $file ( keys %{$filesData} )
  280. {
  281. $filesCount++;
  282. print $fh " <swi:file>\n";
  283. print $fh " <swi:name>" . $file . "</swi:name>\n";
  284. print $fh " <swi:location>" . $file . "</swi:location>\n";
  285. print $fh "\n";
  286. my $functions = $filesData->{$file};
  287. my $localFunctionsCount = 0;
  288. foreach my $function ( keys %$functions )
  289. {
  290. $functionsCount++;
  291. $localFunctionsCount++;
  292. print $fh " <swi:function>\n";
  293. print $fh " " . XMLout( $function, RootName => 'swi:name' );
  294. print $fh " "
  295. . XMLout(
  296. $file . ":"
  297. . ( $functions->{$function}->{'swi:line:headerstart'} + 1 ),
  298. RootName => 'swi:location'
  299. );
  300. print $fh " "
  301. . XMLout( $functions->{$function}->{'swi:modifier'},
  302. RootName => 'swi:modifier' );
  303. print $fh " <swi:pointer"
  304. . " swi:headerstart=\""
  305. . ( $functions->{$function}->{'swi:line:headerstart'} + 1 ) . "\""
  306. . " swi:commentstart=\""
  307. . ( $functions->{$function}->{'swi:line:commentstart'} + 1 )
  308. . "\""
  309. . " swi:blockstart=\""
  310. . ( $functions->{$function}->{'swi:line:blockstart'} + 1 ) . "\""
  311. . " swi:blockend=\""
  312. . ( $functions->{$function}->{'swi:line:blockend'} + 1 ) . "\""
  313. . " />\n";
  314. my $statStr = XMLout(
  315. $functions->{$function}->{'swi:statistic'},
  316. RootName => 'swi:statistic',
  317. GroupTags => {
  318. 'swi:complexity' => '__REMOVE__',
  319. 'swi:duplication' => '__REMOVE__',
  320. 'swi:length' => '__REMOVE__',
  321. 'swi:lines' => '__REMOVE__',
  322. 'swi:checksum' => '__REMOVE__'
  323. }
  324. );
  325. $statStr =~ s/\n/\n /g;
  326. $statStr =~ s/ name="__REMOVE__"//g;
  327. print $fh " ";
  328. print $fh $statStr;
  329. print $fh "\n";
  330. my $refStr = XMLout( $functions->{$function}->{'swi:reference'},
  331. RootName => '' );
  332. $refStr =~ s/\n/\n /g;
  333. $refStr =~ s/<anon /<swi:reference /g;
  334. print $fh " ";
  335. print $fh $refStr;
  336. print $fh " </swi:function>\n";
  337. print $fh "\n";
  338. }
  339. print $fh " <swi:statistic>\n";
  340. print $fh " <swi:count>\n";
  341. print $fh " <swi:functions swi:exact=\""
  342. . $localFunctionsCount
  343. . "\" />\n";
  344. print $fh " </swi:count>\n";
  345. print $fh " </swi:statistic>\n";
  346. print $fh "\n";
  347. print $fh " </swi:file>\n";
  348. print $fh "\n";
  349. }
  350. print $fh " <swi:statistic>\n";
  351. print $fh " <swi:count>\n";
  352. # Warning: swi_merger requires to have counter of files before counters of functions
  353. print $fh " <swi:files swi:exact=\"" . $filesCount . "\" />\n";
  354. print $fh " <swi:functions swi:exact=\""
  355. . $functionsCount
  356. . "\" />\n";
  357. print $fh " </swi:count>\n";
  358. print $fh " </swi:statistic>\n";
  359. print $fh "\n";
  360. print $fh " </swi:module>\n";
  361. print $fh "\n";
  362. print $fh "</swi:report>\n";
  363. $fh->close();
  364. }
  365. #
  366. # Parser enter point
  367. #
  368. sub swiParse
  369. {
  370. my $location = shift();
  371. my $file = shift();
  372. my $preprocessorRules = shift();
  373. my $scanerRules = shift();
  374. my $dupfinderSettings = shift();
  375. STATUS("Parsing file: '$location/$file'.");
  376. my $fh = new FileHandle( $location . "/" . $file, "r" )
  377. or die("Can not open input file '$location/$file'!");
  378. my @fileLines = <$fh>;
  379. # Get all types of strings
  380. my ( $globalBlock_Initial, $globalBlock_Code, $globalBlock_Comment ) =
  381. swiSourceCommentsDeattach(@fileLines);
  382. my $globalBlock_NoStr = swiSourceCodeStringsRemove($globalBlock_Code);
  383. my $globalBlock_NoPrep = swiSourceCodePreprocessorRemove($globalBlock_Code);
  384. my $globalBlock_Purified = swiSourceCodeStringsRemove($globalBlock_NoPrep);
  385. # Preprocess purified code
  386. foreach my $rule ( @{$preprocessorRules} )
  387. {
  388. my $pattern = $rule->{'swi:filepattern'};
  389. if ( $file =~ m/$pattern/ )
  390. {
  391. $globalBlock_Purified = swiSourceCodePreprocess(
  392. $globalBlock_Purified,
  393. $rule->{'swi:searchpattern'},
  394. $rule->{'swi:replacepattern'}
  395. );
  396. }
  397. }
  398. # Parse source code
  399. my $functionsData =
  400. swiSourceCodeParse( $file, $globalBlock_Purified, $globalBlock_Comment );
  401. # Adjusting scaner rules (for performance purposes)
  402. my $scanerRulesFiltered = [];
  403. foreach my $rule ( @{$scanerRules} )
  404. {
  405. my $pattern = $rule->{'swi:filepattern'};
  406. if ( $file =~ m/$pattern/ )
  407. {
  408. push( @{$scanerRulesFiltered}, $rule );
  409. }
  410. }
  411. # Add statistics
  412. $functionsData = swiSourceIndexAdd(
  413. $file, $functionsData, $scanerRulesFiltered,
  414. $dupfinderSettings, $globalBlock_Initial, $globalBlock_Code,
  415. $globalBlock_Comment, $globalBlock_NoPrep, $globalBlock_NoStr,
  416. $globalBlock_Purified
  417. );
  418. return $functionsData;
  419. }
  420. sub swiSourceCodePreprocess
  421. {
  422. my $code = shift();
  423. my $search = shift();
  424. my $replace = shift();
  425. my $lastPart = $code;
  426. my $result = "";
  427. my $posPrev = 0;
  428. my $posCurrent = 0;
  429. while ( $code =~ m/$search/g )
  430. {
  431. $posPrev = $posCurrent;
  432. $posCurrent = pos($code);
  433. my $matchStr = $&;
  434. $lastPart = $';
  435. my $replaceString = eval( 'my $tmp = "' . $replace . '";' );
  436. if ( swiMatchPatternCount( $matchStr, '\n' ) !=
  437. swiMatchPatternCount( $replaceString, '\n' ) )
  438. {
  439. die(
  440. "Wrong preprocessor rule detected in the configuration file: it changes number of lines replacing the text\n>>>\n"
  441. . $matchStr
  442. . "\n<<<\nby\n>>>\n"
  443. . $replaceString
  444. . "\n<<<\n" );
  445. }
  446. my $blockFrag =
  447. substr( $code, $posPrev, $posCurrent - $posPrev - length($matchStr) );
  448. $result .= ( $blockFrag . $replaceString );
  449. }
  450. $result .= $lastPart;
  451. return $result;
  452. }
  453. sub swiSourceCodeScan
  454. {
  455. my $file = shift();
  456. my $offset = shift();
  457. my $code = shift();
  458. my $search = shift();
  459. my $message = shift();
  460. my @result = [];
  461. while ( $code =~ m/$search/g )
  462. {
  463. my $matchPre = $`;
  464. my $matchStr = $&;
  465. my $linePos = swiMatchPatternCount( $matchPre . $matchStr, '\n' ) + 1;
  466. my $messageString = eval( 'my $tmp = "' . $message . '";' );
  467. push(@result, {
  468. 'swi:ref:type' => 'scan',
  469. 'swi:scan:file' => $file,
  470. 'swi:scan:line' => $linePos + $offset,
  471. 'swi:scan:message' => $messageString
  472. });
  473. }
  474. return @result;
  475. }
  476. sub swiSourceCodeGlobalGet
  477. {
  478. my $functionsData = shift();
  479. my $endLine = shift();
  480. my @blockCodeAgr = @_;
  481. my @blockCode;
  482. if ( $#blockCodeAgr == 0 )
  483. {
  484. # Continues code string provided
  485. @blockCode = split( "\n", $blockCodeAgr[0] );
  486. }
  487. else
  488. {
  489. # Array of lines provided
  490. @blockCode = @blockCodeAgr;
  491. }
  492. if ( !defined($endLine) )
  493. {
  494. $endLine = $#blockCode + 100; # Just with overhead
  495. }
  496. foreach my $function ( values %{$functionsData} )
  497. {
  498. for (
  499. my $i = $function->{'swi:line:commentstart'} ;
  500. $i <= $function->{'swi:line:blockend'} ;
  501. $i++
  502. )
  503. {
  504. # Clear line if it is inside function
  505. $blockCode[$i] = "";
  506. }
  507. }
  508. my $result = "";
  509. foreach (@blockCode)
  510. {
  511. $endLine--;
  512. if ( $endLine < 0 )
  513. {
  514. last;
  515. }
  516. $result .= $_ . "\n";
  517. }
  518. return $result;
  519. }
  520. sub swiDupindexFunctionAdd
  521. {
  522. my $fileName = shift();
  523. my $functionName = shift();
  524. my $functionCode = shift();
  525. print $dupindexIn "init_file\n";
  526. print $dupindexIn length($fileName) . "\n";
  527. print $dupindexIn length($functionName) . "\n";
  528. print $dupindexIn length($functionCode) . "\n";
  529. print $dupindexIn $fileName . $functionName . $functionCode . "\n";
  530. }
  531. sub swiSourceIndexAdd
  532. {
  533. my $file = shift();
  534. my $functionsData = shift();
  535. my $scanerRules = shift();
  536. my $dupfinderSettings = shift();
  537. my @block_Initial = split( "\n", shift() );
  538. my @block_Code = split( "\n", shift() );
  539. my @block_Comment = split( "\n", shift() );
  540. my @block_NoPrep = split( "\n", shift() );
  541. my @block_NoStr = split( "\n", shift() );
  542. my $block_Purified = shift();
  543. # Extend arrays in order to remove warnings
  544. my $finalLinesNum = swiMatchPatternCount( $block_Purified, '\n' ) + 1;
  545. @block_Initial = swiUtilArrayExtend( [@block_Initial], $finalLinesNum, "" );
  546. @block_Code = swiUtilArrayExtend( [@block_Code], $finalLinesNum, "" );
  547. @block_Comment = swiUtilArrayExtend( [@block_Comment], $finalLinesNum, "" );
  548. @block_NoPrep = swiUtilArrayExtend( [@block_NoPrep], $finalLinesNum, "" );
  549. @block_NoStr = swiUtilArrayExtend( [@block_NoStr], $finalLinesNum, "" );
  550. foreach my $functionName ( keys %{$functionsData} )
  551. {
  552. my $function = $functionsData->{$functionName};
  553. my $block = {};
  554. $block->{'functionname'} = $functionName;
  555. $block->{'functionname'} =~
  556. s/($regexpCodeContainerDelimeter)?($regexpCodeContainerIdentifier$regexpCodeContainerDelimeter)*($regexpCodeFunctionIdentifier)/$3/;
  557. if ( $functionName ne $regexpCodeGlobalFunctionName )
  558. {
  559. # Get content
  560. $block->{'initial'} = "";
  561. for (
  562. my $i = $function->{'swi:line:commentstart'} ;
  563. $i <= $function->{'swi:line:blockend'} ;
  564. $i++
  565. )
  566. {
  567. $block->{'initial'} .= $block_Initial[$i] . "\n";
  568. }
  569. $block->{'code'} = "";
  570. for (
  571. my $i = $function->{'swi:line:commentstart'} ;
  572. $i <= $function->{'swi:line:blockend'} ;
  573. $i++
  574. )
  575. {
  576. $block->{'code'} .= $block_Code[$i] . "\n";
  577. }
  578. $block->{'comments'} = "";
  579. for (
  580. my $i = $function->{'swi:line:commentstart'} ;
  581. $i <= $function->{'swi:line:blockend'} ;
  582. $i++
  583. )
  584. {
  585. $block->{'comments'} .= $block_Comment[$i] . "\n";
  586. }
  587. $block->{'nopreprocessor'} = "";
  588. for (
  589. my $i = $function->{'swi:line:commentstart'} ;
  590. $i <= $function->{'swi:line:blockend'} ;
  591. $i++
  592. )
  593. {
  594. $block->{'nopreprocessor'} .= $block_NoPrep[$i] . "\n";
  595. }
  596. $block->{'nostrings'} = "";
  597. for (
  598. my $i = $function->{'swi:line:commentstart'} ;
  599. $i <= $function->{'swi:line:blockend'} ;
  600. $i++
  601. )
  602. {
  603. $block->{'nostrings'} .= $block_NoStr[$i] . "\n";
  604. }
  605. $block->{'purified'} = substr(
  606. $block_Purified,
  607. $function->{'swi:pos:headerstart'},
  608. $function->{'swi:pos:blockend'} -
  609. $function->{'swi:pos:headerstart'}
  610. )
  611. . "\n";
  612. $block->{'commentshead'} = "";
  613. for (
  614. my $i = $function->{'swi:line:commentstart'} ;
  615. $i < $function->{'swi:line:headerstart'} ;
  616. $i++
  617. )
  618. {
  619. $block->{'commentshead'} .= $block_Comment[$i] . "\n";
  620. }
  621. # functionname created above
  622. $block->{'functionhead'} = substr(
  623. $block_Purified,
  624. $function->{'swi:pos:headerstart'},
  625. $function->{'swi:pos:blockstart'} -
  626. $function->{'swi:pos:headerstart'}
  627. )
  628. . "\n";
  629. $block->{'functionbody'} = substr(
  630. $block_Purified,
  631. $function->{'swi:pos:blockstart'},
  632. $function->{'swi:pos:blockend'} -
  633. $function->{'swi:pos:blockstart'}
  634. )
  635. . "\n";
  636. }
  637. else
  638. {
  639. $block->{'initial'} =
  640. swiSourceCodeGlobalGet( $functionsData, undef, @block_Initial );
  641. $block->{'code'} =
  642. swiSourceCodeGlobalGet( $functionsData, undef, @block_Code );
  643. $block->{'comments'} =
  644. swiSourceCodeGlobalGet( $functionsData, undef, @block_Comment );
  645. $block->{'nopreprocessor'} =
  646. swiSourceCodeGlobalGet( $functionsData, undef, @block_NoPrep );
  647. $block->{'nostrings'} =
  648. swiSourceCodeGlobalGet( $functionsData, undef, @block_NoStr );
  649. $block->{'purified'} =
  650. swiSourceCodeGlobalGet( $functionsData, undef, $block_Purified );
  651. $block->{'commentshead'} =
  652. swiSourceCodeGlobalGet( $functionsData,
  653. $function->{'swi:line:headerstart'},
  654. @block_Comment );
  655. }
  656. # Initialize container
  657. $function->{'swi:statistic'} = {
  658. 'swi:length' => {
  659. 'swi:source' => {},
  660. 'swi:executable' => {},
  661. 'swi:comment' => {},
  662. 'swi:blank' => {},
  663. 'swi:function:name' => {}
  664. },
  665. 'swi:lines' => {
  666. 'swi:source' => {},
  667. 'swi:executable' => {},
  668. 'swi:comment' => {},
  669. 'swi:blank' => {},
  670. 'swi:comment:header' => {}
  671. },
  672. 'swi:complexity' => {
  673. 'swi:cyclomatic' => {},
  674. 'swi:blocks' => {},
  675. 'swi:maxdepth' => {}
  676. },
  677. 'swi:duplication' => { 'swi:symbols' => { 'swi:exact' => 0 }, },
  678. 'swi:checksum' => { 'swi:source' => { 'swi:exact' => 0 }, }
  679. };
  680. $function->{'swi:reference'} = [];
  681. # Calculate swi:length->swi:source
  682. $function->{'swi:statistic'}->{'swi:length'}->{'swi:source'}
  683. ->{'swi:exact'} = length( $block->{'initial'} );
  684. # Calculate swi:length->swi:executable
  685. $function->{'swi:statistic'}->{'swi:length'}->{'swi:executable'}
  686. ->{'swi:exact'} = length( $block->{'purified'} );
  687. # Calculate swi:length->swi:comment
  688. $function->{'swi:statistic'}->{'swi:length'}->{'swi:comment'}
  689. ->{'swi:exact'} = length( $block->{'comments'} );
  690. # Calculate swi:length->swi:blank
  691. $function->{'swi:statistic'}->{'swi:length'}->{'swi:blank'}
  692. ->{'swi:exact'} = swiMatchPatternCount( $block->{'initial'}, '\s' );
  693. # Calculate swi:length->swi:function-name
  694. $function->{'swi:statistic'}->{'swi:length'}->{'swi:function:name'}
  695. ->{'swi:exact'} = length( $block->{'functionname'} );
  696. # Calculate swi:lines->swi:source
  697. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:source'}
  698. ->{'swi:exact'} = swiMatchPatternCount( $block->{'initial'}, '\n' );
  699. # Calculate swi:lines->swi:executable
  700. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:executable'}
  701. ->{'swi:exact'} =
  702. swiMatchLinesCount( $block->{'purified'}, '[^ \t]' );
  703. # Calculate swi:lines->swi:comment
  704. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:comment'}
  705. ->{'swi:exact'} =
  706. swiMatchLinesCount( $block->{'comments'}, '[^ \t]' );
  707. # Calculate swi:lines->swi:blank
  708. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:blank'}
  709. ->{'swi:exact'} = swiMatchLinesCount( $block->{'initial'}, '^\s*$' );
  710. # Calculate swi:lines->swi:comment:header
  711. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:comment:header'}
  712. ->{'swi:exact'} =
  713. $function->{'swi:line:headerstart'} -
  714. $function->{'swi:line:commentstart'};
  715. # Calculate swi:complexity->swi:cyclomatic
  716. $function->{'swi:statistic'}->{'swi:complexity'}->{'swi:cyclomatic'}
  717. ->{'swi:exact'} =
  718. swiMatchPatternCount(
  719. swiSourceCodeStringsRemove( $block->{'purified'} ),
  720. $regexpCodeMatch_ComplexityCyclomatic );
  721. # Calculate swi:complexity->swi:blocks
  722. # -1 in order not to count main function's block
  723. $function->{'swi:statistic'}->{'swi:complexity'}->{'swi:blocks'}
  724. ->{'swi:exact'} =
  725. swiMatchPatternCount( $block->{'purified'}, $regexpCodeBlockStart ) -
  726. 1;
  727. # Calculate swi:complexity->swi:maxdepth
  728. $function->{'swi:statistic'}->{'swi:complexity'}->{'swi:maxdepth'}
  729. ->{'swi:exact'} = $function->{'swi:depth'};
  730. # Calculate swi:checksum->swi:index
  731. my @symbols = split( //, $block->{'initial'} );
  732. my $crcSumTotal = 0;
  733. for ( my $pos = 0 ; $pos <= $#symbols ; $pos++ )
  734. {
  735. $crcSumTotal =
  736. ( $crcSumTotal << 8 )
  737. ^ $crctab[ ( $crcSumTotal >> 24 ) ^ ( unpack 'C', $symbols[$pos] )
  738. ];
  739. }
  740. $function->{'swi:statistic'}->{'swi:checksum'}->{'swi:source'}
  741. ->{'swi:exact'} = $crcSumTotal;
  742. # Scan function by scaner tool
  743. foreach my $rule ( @{$scanerRules} )
  744. {
  745. if ( defined( $block->{ $rule->{'swi:codecontent'} } ) )
  746. {
  747. push(
  748. @{ $function->{'swi:reference'} },
  749. swiSourceCodeScan(
  750. $file, $function->{'swi:line:headerstart'},
  751. $block->{ $rule->{'swi:codecontent'} },
  752. $rule->{'swi:searchpattern'},
  753. $rule->{'swi:messagepattern'}
  754. )
  755. );
  756. }
  757. else
  758. {
  759. STATUS(
  760. "Wrong configuiration: 'swi:scaner/swi:rule/swi:codecontent' property is not from the acceptable set of values!"
  761. );
  762. $exitCode++;
  763. }
  764. }
  765. # Add to dupindexer for further processing
  766. if ( defined( $block->{ $dupfinderSettings->{'swi:codecontent'} } ) )
  767. {
  768. if (
  769. $functionName ne $regexpCodeGlobalFunctionName
  770. || ( defined( $dupfinderSettings->{'swi:globalcode'} )
  771. && $dupfinderSettings->{'swi:globalcode'} eq 'on' )
  772. )
  773. {
  774. swiDupindexFunctionAdd( $file, $functionName,
  775. $block->{ $dupfinderSettings->{'swi:codecontent'} } );
  776. }
  777. }
  778. else
  779. {
  780. STATUS(
  781. "Wrong configuiration: 'swi:indexer:dup/swi:codecontent' property is not from the acceptable set of values!"
  782. );
  783. $exitCode++;
  784. }
  785. }
  786. return $functionsData;
  787. }
  788. #
  789. # This function uses external tool for duplication collection
  790. #
  791. sub swiSourceIndexDuplicationAdd
  792. {
  793. my $filesData = shift();
  794. STATUS("Searching for duplication...");
  795. print $dupindexIn "start\n";
  796. my $outStream = "";
  797. while ( defined($dupindexOut) && ( my $line = <$dupindexOut> ) )
  798. {
  799. $outStream .= $line;
  800. }
  801. my @dupdataGoups = split( /info: group_start/, $outStream );
  802. foreach (@dupdataGoups)
  803. {
  804. my @parsedData;
  805. my @dupdataSplit = split( /\n/, $_ );
  806. foreach (@dupdataSplit)
  807. {
  808. if ( $_ =~
  809. m/duplication: file: '(.*)' function: '(.*)' possition: '(.*)' size: '(.*)'/
  810. )
  811. {
  812. $filesData->{$1}->{$2}->{'swi:statistic'}->{'swi:duplication'}
  813. ->{'swi:symbols'}->{'swi:exact'} += $4;
  814. push(
  815. @parsedData,
  816. {
  817. 'swi:ref:type' => 'dup',
  818. 'swi:dup:file' => $1,
  819. 'swi:dup:function' => $2,
  820. 'swi:dup:line' => $3 +
  821. $filesData->{$1}->{$2}->{'swi:line:headerstart'},
  822. 'swi:dup:size' => $4
  823. }
  824. );
  825. }
  826. }
  827. foreach my $dupData (@parsedData)
  828. {
  829. push(
  830. @{
  831. $filesData->{ $dupData->{'swi:dup:file'} }
  832. ->{ $dupData->{'swi:dup:function'} }->{'swi:reference'}
  833. },
  834. @parsedData
  835. );
  836. }
  837. }
  838. my $errStream = "";
  839. while ( defined($dupindexErr) && ( my $line = <$dupindexErr> ) )
  840. {
  841. STATUS("Internal dupindex tool detected the error.");
  842. STATUS($line);
  843. STATUS(" 1. Check 'swi:indexer:dup' section in configuration.");
  844. STATUS(
  845. " 2. Check that swi/dupindex tool is runable on your system/platform."
  846. );
  847. STATUS(" 3. Recompile swi/dupindex tool for your system/platform.");
  848. STATUS(" 4. Report the problem to developers.");
  849. # The final return code is unsuccessful
  850. # It means that these errors/warnings should be fixed before the next processing
  851. $exitCode++;
  852. }
  853. return $filesData;
  854. }
  855. sub swiSourceCodeParse
  856. {
  857. my $file = shift();
  858. my $blockCode = shift();
  859. my @blockComment = split( "\n", shift() );
  860. @blockComment =
  861. swiUtilArrayExtend( [@blockComment],
  862. swiMatchPatternCount( $blockCode, '\n' ) + 1, "" );
  863. my $result = {};
  864. my $deepLevel = 0;
  865. if ( swiMatchPatternCount( $blockCode, $regexpCodeBlockStart ) !=
  866. swiMatchPatternCount( $blockCode, $regexpCodeBlockEnd ) )
  867. {
  868. PRINT( $file, 0, "error",
  869. "Mismatch in number of start/end of blocks delimeters!\n" );
  870. DEBUG("The code before parsing:\n$blockCode");
  871. return $result;
  872. }
  873. my $posPrev = 0;
  874. my $posCurrent = 0;
  875. my $isInFunction = 0;
  876. my $currentFunc = undef;
  877. my $currentLine = 0;
  878. my $currentPos = 0;
  879. my $currentDepth = 0;
  880. my @containers;
  881. while ( $blockCode =~ m/($regexpCodeBlockStart)|($regexpCodeBlockEnd)/g )
  882. {
  883. $posPrev = $posCurrent;
  884. $posCurrent = pos($blockCode);
  885. my $matchPre = $`;
  886. my $matchStr = $&;
  887. my $matchPost = $';
  888. my $blockFragFull =
  889. substr( $blockCode, $posPrev, $posCurrent - $posPrev );
  890. $currentLine += swiMatchPatternCount( $blockFragFull, "\n" );
  891. $currentPos += length($blockFragFull);
  892. if ( $matchStr =~ m/^$regexpCodeBlockStart$/ )
  893. {
  894. my $blockFragLast = $blockFragFull;
  895. $blockFragLast =~ s/^($regexpCodeStatements)*\s*//;
  896. # opened block detected
  897. $deepLevel++;
  898. if ( $isInFunction == 0 )
  899. {
  900. # Check whether it is a function start
  901. if ( $blockFragLast =~
  902. m/^($regexpCodeFunctionModifier)*($regexpCodeFunctionIdentifier)($regexpCodeFunctionArguments)($regexpCodeBlockStart)$/
  903. )
  904. {
  905. my $matchedHeader = $&;
  906. my $mod = $1;
  907. my $word = $2;
  908. if ( !defined($mod) )
  909. {
  910. $mod = "";
  911. }
  912. # Remove empty symbols in header
  913. $matchedHeader =~ s/^\s*//;
  914. # Check if modifier consumes something from identifier
  915. # and return back the last part from modifier to identifier
  916. ( $mod . $word ) =~ m/$regexpCodeFunctionIdentifier$/;
  917. $mod = $`;
  918. $word = $&;
  919. # Remove empty symbols in indetifier
  920. $word =~ s/[\n\t ]+/ /g;
  921. # Remove empty symbols in modifier
  922. $mod =~ s/^\s*//;
  923. $mod =~ s/\s*$//;
  924. $mod =~ s/[\n\t ]+/ /g;
  925. if ( $word !~ m/^($regexpCodeKeyword)$/ )
  926. {
  927. $isInFunction = $deepLevel;
  928. # Attach containers to the name of function
  929. foreach my $container ( reverse @containers )
  930. {
  931. if ( defined($container) )
  932. {
  933. $word = $container
  934. . $regexpCodeContainerDelimeter
  935. . $word;
  936. }
  937. }
  938. # Add the code before the block start
  939. # (name of function, arguments, ..)
  940. my $matchedHeaderLinesNum =
  941. swiMatchPatternCount( $matchedHeader, "\n" );
  942. # Check if there are comments before the header
  943. my $consumedTopLines = 1;
  944. while (
  945. $currentLine - $matchedHeaderLinesNum -
  946. $consumedTopLines >= 0
  947. && $blockComment[
  948. $currentLine - $matchedHeaderLinesNum -
  949. $consumedTopLines
  950. ] !~ m/^\s*$/
  951. )
  952. {
  953. $consumedTopLines++;
  954. }
  955. if ( defined( $result->{$word} ) )
  956. {
  957. PRINT( $file, $currentLine + 1, 'debug',
  958. "The same function detected more than once: '$word'"
  959. );
  960. my $counter = 2;
  961. while (
  962. defined( $result->{ $word . " (" . $counter . ")" } ) )
  963. {
  964. $counter++;
  965. }
  966. $word .= " (" . $counter . ")";
  967. }
  968. # Store data in result
  969. $currentFunc = $word;
  970. $result->{$word} = {
  971. 'swi:pos:blockstart' => $currentPos,
  972. 'swi:pos:headerstart' => $currentPos -
  973. length($matchedHeader),
  974. 'swi:pos:blockend' => undef,
  975. 'swi:line:blockstart' => $currentLine,
  976. 'swi:line:headerstart' => $currentLine -
  977. $matchedHeaderLinesNum,
  978. 'swi:line:commentstart' => $currentLine -
  979. $matchedHeaderLinesNum -
  980. ( $consumedTopLines - 1 ),
  981. 'swi:line:blockend' => undef,
  982. 'swi:modifier' => $mod,
  983. 'swi:indent' => $deepLevel
  984. };
  985. PRINT( $file, $currentLine + 1, 'debug',
  986. "Function detected: '$word', with modifier: '$mod'"
  987. );
  988. }
  989. }
  990. # Check whether it is a container start
  991. elsif ( $blockFragLast =~
  992. m/^($regexpCodeContainerModifier)\s+($regexpCodeContainerIdentifier)($regexpCodeContainerArguments)($regexpCodeBlockStart)$/
  993. )
  994. {
  995. my $mod = $1;
  996. my $word = $2;
  997. $mod =~ s/^\s*//;
  998. $mod =~ s/\s*$//;
  999. $mod =~ s/[\n\t ]+/ /g;
  1000. if ( $word !~ m/^$regexpCodeKeyword$/ )
  1001. {
  1002. PRINT( $file, $currentLine + 1, 'debug',
  1003. "Container detected: '$word', with modifier: '$mod'"
  1004. );
  1005. $containers[ $deepLevel - 1 ] = $word;
  1006. }
  1007. }
  1008. # Check if block start should be ignored
  1009. elsif ( $blockFragLast =~
  1010. m/($regexpCodeBlockStartIgnore)($regexpCodeBlockStart)$/ )
  1011. {
  1012. PRINT( $file, $currentLine + 1,
  1013. 'debug', "Block start delimeter ignored/missed: '$&'" );
  1014. }
  1015. elsif ( $blockFragLast =~ m/^\s*($regexpCodeBlockStart)$/ )
  1016. {
  1017. PRINT( $file, $currentLine + 1, 'debug',
  1018. "Empty code before block start delimeter detected: '$&'"
  1019. );
  1020. }
  1021. else
  1022. {
  1023. $blockFragLast =~ s/^\s*//;
  1024. PRINT( $file, $currentLine + 1,
  1025. 'error',
  1026. "Unknown type of started block: '$blockFragLast'" );
  1027. $exitCode++;
  1028. }
  1029. }
  1030. else
  1031. {
  1032. # Calculate maximum indent level
  1033. if ( $currentDepth < $deepLevel - $isInFunction )
  1034. {
  1035. $currentDepth = $deepLevel - $isInFunction;
  1036. }
  1037. }
  1038. }
  1039. else
  1040. {
  1041. # block closer detected
  1042. if ( $deepLevel == $isInFunction )
  1043. {
  1044. $isInFunction = 0;
  1045. $result->{$currentFunc}->{'swi:pos:blockend'} = $currentPos;
  1046. $result->{$currentFunc}->{'swi:line:blockend'} = $currentLine;
  1047. $result->{$currentFunc}->{'swi:depth'} = $currentDepth;
  1048. $currentDepth = 0;
  1049. $currentFunc = undef;
  1050. }
  1051. $containers[ $deepLevel - 1 ] = undef;
  1052. $deepLevel--;
  1053. }
  1054. }
  1055. # Initialize GLOBAL function
  1056. $result->{$regexpCodeGlobalFunctionName} = {
  1057. 'swi:pos:blockstart' => 0, # not used
  1058. 'swi:pos:headerstart' => 0, # not used
  1059. 'swi:pos:blockend' => 0, # not used,
  1060. 'swi:line:blockstart' => 0, # not used
  1061. 'swi:line:headerstart' => 0, # used to deattach header with comments
  1062. 'swi:line:commentstart' => 0, # not used
  1063. 'swi:line:blockend' => 0, # not used
  1064. 'swi:modifier' => "", # not used
  1065. 'swi:indent' => 0, # not used
  1066. 'swi:depth' => 0 # not used
  1067. };
  1068. for ( my $i = 0 ; $i <= $#blockComment ; ++$i )
  1069. {
  1070. $result->{$regexpCodeGlobalFunctionName}->{'swi:line:headerstart'} = $i;
  1071. if ( $blockComment[$i] =~ m/^\s*$/ )
  1072. {
  1073. # Empty line detected => header end matched
  1074. last;
  1075. }
  1076. }
  1077. return $result;
  1078. }
  1079. sub swiSourceCodeStringsRemove
  1080. {
  1081. my $code = shift();
  1082. my $result = "";
  1083. my $posPrev = 0;
  1084. my $posCurrent = 0;
  1085. my $isInString = 0;
  1086. my $lastPart = $code;
  1087. my $borderPatternDynamic = $regexpCodeStringBorder_Text;
  1088. while ( $code =~ m/$borderPatternDynamic/g )
  1089. {
  1090. $posPrev = $posCurrent;
  1091. $posCurrent = pos($code);
  1092. my $matchPre = $`;
  1093. my $matchStr = $&;
  1094. my $matchPost = $';
  1095. $lastPart = "";
  1096. my $blockFrag =
  1097. substr( $code, $posPrev, $posCurrent - $posPrev - length($matchStr) );
  1098. if ( $isInString == 0 )
  1099. {
  1100. $result .= $blockFrag . $matchStr;
  1101. $isInString = 1;
  1102. $borderPatternDynamic = $matchStr;
  1103. }
  1104. else
  1105. {
  1106. if ( ( $blockFrag . $matchStr ) =~
  1107. m/($regexpCodeStringBorder_Escape)($borderPatternDynamic)$/ )
  1108. {
  1109. # This string border should be ignored
  1110. $blockFrag =~ s/[^\n]//g;
  1111. $result .= $blockFrag;
  1112. }
  1113. else
  1114. {
  1115. # Save last part only if string end detected;
  1116. $lastPart = $matchPost;
  1117. # Keep newlines in the code inside strings
  1118. $blockFrag =~ s/[^\n]//g;
  1119. $result .= $blockFrag;
  1120. $result .= $matchStr;
  1121. $isInString = 0;
  1122. $borderPatternDynamic = $regexpCodeStringBorder_Text;
  1123. }
  1124. }
  1125. }
  1126. return $result . $lastPart;
  1127. }
  1128. sub swiSourceCodePreprocessorRemove
  1129. {
  1130. my $code = shift();
  1131. my $result = "";
  1132. my @codeLines = swiUtilArrayExtend( [ split( "\n", $code ) ],
  1133. swiMatchPatternCount( $code, '\n' ) - 1, "" );
  1134. my $isInPreprocessor = 0;
  1135. foreach my $line (@codeLines)
  1136. {
  1137. if ( $isInPreprocessor == 1
  1138. || $line =~ m/$regexpCodePreprocessorStart/ )
  1139. {
  1140. $isInPreprocessor = 1;
  1141. # Just add empty line
  1142. $result .= "\n";
  1143. if ( $line =~ m/$regexpCodePreprocessorEnd/ )
  1144. {
  1145. $isInPreprocessor = 0;
  1146. }
  1147. }
  1148. else
  1149. {
  1150. $line =~ s/\n//g;
  1151. $result .= $line . "\n";
  1152. }
  1153. }
  1154. return $result;
  1155. }
  1156. sub swiSourceCommentsDeattach
  1157. {
  1158. my @fileLines = @_;
  1159. my $blockAll = "";
  1160. my $blockCode = "";
  1161. my $blockComment = "";
  1162. my $countLinesAll = 0;
  1163. my $countLinesCode = 0;
  1164. my $countLinesComment = 0;
  1165. my $isInCode = 1;
  1166. foreach my $line (@fileLines)
  1167. {
  1168. if ( $isInCode == 1 )
  1169. {
  1170. if ( $line =~
  1171. m/(($regexpCommentMultiStart)|($regexpCommentSingle))/ )
  1172. {
  1173. my $matchPre = $`;
  1174. my $matchStr = $&;
  1175. my $matchPost = $';
  1176. my $marker = "#@%" . int( rand(0xFF) ) . "%@#";
  1177. if ( swiSourceCodeStringsRemove( $matchPre . $marker ) !~
  1178. m/$marker/ )
  1179. {
  1180. # This block ends inside the string
  1181. # i.e. comment start marker detected inside the string
  1182. # Process again the string with removed comment start marker
  1183. $line =~
  1184. s/(($regexpCommentMultiStart)|($regexpCommentSingle))//;
  1185. $fileLines[$countLinesAll] = $line;
  1186. redo;
  1187. }
  1188. elsif ( $matchStr =~ m/^$regexpCommentMultiStart$/ )
  1189. {
  1190. # Get the code before the comment
  1191. $blockCode .= $matchPre;
  1192. $blockAll .= $matchPre;
  1193. $isInCode = 0;
  1194. $blockComment .= $matchStr;
  1195. $blockAll .= $matchStr;
  1196. # Process again the last fragment
  1197. $fileLines[$countLinesAll] = $matchPost;
  1198. $line = $matchPost;
  1199. redo;
  1200. }
  1201. else
  1202. {
  1203. # Get the code before the comment
  1204. $blockCode .= $matchPre;
  1205. $blockAll .= $matchPre;
  1206. # Single line comment detected
  1207. $matchPost =~ s/\n//g;
  1208. $blockComment .= $matchStr . $matchPost;
  1209. $blockAll .= $matchStr . $matchPost;
  1210. }
  1211. }
  1212. else
  1213. {
  1214. $line =~ s/\n//g;
  1215. $blockCode .= $line;
  1216. $blockAll .= $line;
  1217. }
  1218. }
  1219. else
  1220. {
  1221. # Check multi line comment end
  1222. if ( $line =~ m/$regexpCommentMultiEnd/ )
  1223. {
  1224. my $matchPre = $`;
  1225. my $matchStr = $&;
  1226. my $matchPost = $';
  1227. # Get the code before the comment
  1228. $blockComment .= $matchPre . $matchStr;
  1229. $blockAll .= $matchPre . $matchStr;
  1230. $isInCode = 1;
  1231. # Process again the last fragment
  1232. $fileLines[$countLinesAll] = $matchPost;
  1233. $line = $matchPost;
  1234. redo;
  1235. }
  1236. else
  1237. {
  1238. $line =~ s/\n//g;
  1239. $blockComment .= $line;
  1240. $blockAll .= $line;
  1241. }
  1242. }
  1243. $blockCode .= "\n";
  1244. $blockComment .= "\n";
  1245. $blockAll .= "\n";
  1246. $countLinesAll++;
  1247. }
  1248. return ( $blockAll, $blockCode, $blockComment );
  1249. }
  1250. sub swiMatchPatternCount
  1251. {
  1252. my $text = shift();
  1253. my $pattern = shift();
  1254. my $count = 0;
  1255. while ( $text =~ m/$pattern/g )
  1256. {
  1257. $count++;
  1258. }
  1259. return $count;
  1260. }
  1261. sub swiMatchLinesCount
  1262. {
  1263. my @text = split( "\n", shift() );
  1264. my $pattern = shift();
  1265. my $count = 0;
  1266. foreach (@text)
  1267. {
  1268. if ( $_ =~ m/$pattern/ )
  1269. {
  1270. $count++;
  1271. }
  1272. }
  1273. return $count;
  1274. }
  1275. sub swiUtilArrayExtend
  1276. {
  1277. my $array = shift();
  1278. my $finalLength = shift();
  1279. my $emptySymbol = shift();
  1280. if ( $#{$array} > $finalLength )
  1281. {
  1282. DEBUG( "Length of array: " . $#{$array} );
  1283. DEBUG( "Requested final length: " . $finalLength );
  1284. DEBUG( Dumper($array) );
  1285. die('Internal Error occured!');
  1286. }
  1287. while ( $finalLength - $#{$array} > 0 )
  1288. {
  1289. push( @{$array}, $emptySymbol );
  1290. }
  1291. return @{$array};
  1292. }
  1293. sub swiUtilDirectoryCleanUp
  1294. {
  1295. my $dir = shift();
  1296. die("Internal error!") if not defined $dir;
  1297. die("Internal error!") if $dir eq "";
  1298. die("Internal error!") if $dir eq ".";
  1299. die("Internal error!") if $dir eq "./";
  1300. if ( opendir( DIR, $dir ) )
  1301. {
  1302. while ( my $file = readdir(DIR) )
  1303. {
  1304. unlink $dir . "/" . $file;
  1305. }
  1306. closedir(DIR);
  1307. }
  1308. }
  1309. return 1;