Processor.pm 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 ( defined($swiGlobalExclude)
  217. && $swiGlobalExclude ne ""
  218. && $file =~ m/$swiGlobalExclude/ )
  219. {
  220. next;
  221. }
  222. $filesData->{$file} = swiParse(
  223. $swiGlobalDirectory,
  224. $file,
  225. $swiGlobalPreprocessorRules,
  226. $swiGlobalScanerRules,
  227. $config->{"swi:modules"}->{"swi:module"}[$moduleId]
  228. ->{"swi:indexer:dup"}
  229. );
  230. }
  231. }
  232. closedir(DIR);
  233. # Add duplication statistics
  234. if ( defined($swiGlobalDupfinderEnabled)
  235. && $swiGlobalDupfinderEnabled eq 'on' )
  236. {
  237. $filesData = swiSourceIndexDuplicationAdd($filesData);
  238. }
  239. else
  240. {
  241. print $dupindexIn "exit\n";
  242. }
  243. # Prepare XML report
  244. STATUS("Generating the report '$swiGlobalReportLocation'...");
  245. swiXmlReportPrint(
  246. $filesData, $swiGlobalDirectory,
  247. $swiGlobalReportLocation, $swiGlobalModuleName
  248. );
  249. $returnCode = $exitCode;
  250. }
  251. return $returnCode;
  252. }
  253. sub swiXmlReportPrint()
  254. {
  255. my $filesData = shift();
  256. my $curDir = shift();
  257. my $reportLocation = shift();
  258. my $moduleName = shift();
  259. $curDir =~ s/\n$//;
  260. my $fh = new FileHandle( "$reportLocation", "w" )
  261. or die("Can not open output file: $reportLocation!");
  262. print $fh "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  263. print $fh "<swi:report>\n";
  264. print $fh "\n";
  265. print $fh " <swi:module>\n";
  266. print $fh " <swi:name>" . $moduleName . "</swi:name>\n";
  267. print $fh " <swi:location>" . $curDir . "</swi:location>\n";
  268. print $fh "\n";
  269. print $fh " <swi:info>\n";
  270. print $fh " <swi:version>1</swi:version>\n";
  271. if ( defined( $ENV{USER} ) )
  272. {
  273. print $fh " <swi:user>" . $ENV{USER} . "</swi:user>\n";
  274. }
  275. print $fh " <swi:generator>SWI/PROCESSOR</swi:generator>\n";
  276. print $fh " </swi:info>\n";
  277. print $fh "\n";
  278. my $filesCount = 0;
  279. my $functionsCount = 0;
  280. foreach my $file ( keys %{$filesData} )
  281. {
  282. $filesCount++;
  283. print $fh " <swi:file>\n";
  284. print $fh " <swi:name>" . $file . "</swi:name>\n";
  285. print $fh " <swi:location>" . $file . "</swi:location>\n";
  286. print $fh "\n";
  287. my $functions = $filesData->{$file};
  288. my $localFunctionsCount = 0;
  289. foreach my $function ( keys %$functions )
  290. {
  291. $functionsCount++;
  292. $localFunctionsCount++;
  293. print $fh " <swi:function>\n";
  294. print $fh " " . XMLout( $function, RootName => 'swi:name' );
  295. print $fh " "
  296. . XMLout(
  297. $file . ":"
  298. . ( $functions->{$function}->{'swi:line:headerstart'} + 1 ),
  299. RootName => 'swi:location'
  300. );
  301. print $fh " "
  302. . XMLout( $functions->{$function}->{'swi:modifier'},
  303. RootName => 'swi:modifier' );
  304. print $fh " <swi:pointer"
  305. . " swi:headerstart=\""
  306. . ( $functions->{$function}->{'swi:line:headerstart'} + 1 ) . "\""
  307. . " swi:commentstart=\""
  308. . ( $functions->{$function}->{'swi:line:commentstart'} + 1 )
  309. . "\""
  310. . " swi:blockstart=\""
  311. . ( $functions->{$function}->{'swi:line:blockstart'} + 1 ) . "\""
  312. . " swi:blockend=\""
  313. . ( $functions->{$function}->{'swi:line:blockend'} + 1 ) . "\""
  314. . " />\n";
  315. my $statStr = XMLout(
  316. $functions->{$function}->{'swi:statistic'},
  317. RootName => 'swi:statistic',
  318. GroupTags => {
  319. 'swi:complexity' => '__REMOVE__',
  320. 'swi:duplication' => '__REMOVE__',
  321. 'swi:length' => '__REMOVE__',
  322. 'swi:lines' => '__REMOVE__',
  323. 'swi:checksum' => '__REMOVE__'
  324. }
  325. );
  326. $statStr =~ s/\n/\n /g;
  327. $statStr =~ s/ name="__REMOVE__"//g;
  328. print $fh " ";
  329. print $fh $statStr;
  330. print $fh "\n";
  331. my $refStr = XMLout( $functions->{$function}->{'swi:reference'},
  332. RootName => '' );
  333. $refStr =~ s/\n/\n /g;
  334. $refStr =~ s/<anon /<swi:reference /g;
  335. print $fh " ";
  336. print $fh $refStr;
  337. print $fh " </swi:function>\n";
  338. print $fh "\n";
  339. }
  340. print $fh " <swi:statistic>\n";
  341. print $fh " <swi:count>\n";
  342. print $fh " <swi:functions swi:exact=\""
  343. . $localFunctionsCount
  344. . "\" />\n";
  345. print $fh " </swi:count>\n";
  346. print $fh " </swi:statistic>\n";
  347. print $fh "\n";
  348. print $fh " </swi:file>\n";
  349. print $fh "\n";
  350. }
  351. print $fh " <swi:statistic>\n";
  352. print $fh " <swi:count>\n";
  353. # Warning: swi_merger requires to have counter of files before counters of functions
  354. print $fh " <swi:files swi:exact=\"" . $filesCount . "\" />\n";
  355. print $fh " <swi:functions swi:exact=\""
  356. . $functionsCount
  357. . "\" />\n";
  358. print $fh " </swi:count>\n";
  359. print $fh " </swi:statistic>\n";
  360. print $fh "\n";
  361. print $fh " </swi:module>\n";
  362. print $fh "\n";
  363. print $fh "</swi:report>\n";
  364. $fh->close();
  365. }
  366. #
  367. # Parser enter point
  368. #
  369. sub swiParse
  370. {
  371. my $location = shift();
  372. my $file = shift();
  373. my $preprocessorRules = shift();
  374. my $scanerRules = shift();
  375. my $dupfinderSettings = shift();
  376. STATUS("Parsing file: '$location/$file'.");
  377. my $fh = new FileHandle( $location . "/" . $file, "r" )
  378. or die("Can not open input file '$location/$file'!");
  379. my @fileLines = <$fh>;
  380. # Get all types of strings
  381. my ( $globalBlock_Initial, $globalBlock_Code, $globalBlock_Comment ) =
  382. swiSourceCommentsDeattach(@fileLines);
  383. my $globalBlock_NoStr = swiSourceCodeStringsRemove($globalBlock_Code);
  384. my $globalBlock_NoPrep = swiSourceCodePreprocessorRemove($globalBlock_Code);
  385. my $globalBlock_Purified = swiSourceCodeStringsRemove($globalBlock_NoPrep);
  386. # Preprocess purified code
  387. foreach my $rule ( @{$preprocessorRules} )
  388. {
  389. my $pattern = $rule->{'swi:filepattern'};
  390. if ( $file =~ m/$pattern/ )
  391. {
  392. $globalBlock_Purified = swiSourceCodePreprocess(
  393. $globalBlock_Purified,
  394. $rule->{'swi:searchpattern'},
  395. $rule->{'swi:replacepattern'}
  396. );
  397. }
  398. }
  399. # Parse source code
  400. my $functionsData =
  401. swiSourceCodeParse( $file, $globalBlock_Purified, $globalBlock_Comment );
  402. # Adjusting scaner rules (for performance purposes)
  403. my $scanerRulesFiltered = [];
  404. foreach my $rule ( @{$scanerRules} )
  405. {
  406. my $pattern = $rule->{'swi:filepattern'};
  407. if ( $file =~ m/$pattern/ )
  408. {
  409. push( @{$scanerRulesFiltered}, $rule );
  410. }
  411. }
  412. # Add statistics
  413. $functionsData = swiSourceIndexAdd(
  414. $file, $functionsData, $scanerRulesFiltered,
  415. $dupfinderSettings, $globalBlock_Initial, $globalBlock_Code,
  416. $globalBlock_Comment, $globalBlock_NoPrep, $globalBlock_NoStr,
  417. $globalBlock_Purified
  418. );
  419. return $functionsData;
  420. }
  421. sub swiSourceCodePreprocess
  422. {
  423. my $code = shift();
  424. my $search = shift();
  425. my $replace = shift();
  426. my $lastPart = $code;
  427. my $result = "";
  428. my $posPrev = 0;
  429. my $posCurrent = 0;
  430. while ( $code =~ m/$search/g )
  431. {
  432. $posPrev = $posCurrent;
  433. $posCurrent = pos($code);
  434. my $matchStr = $&;
  435. $lastPart = $';
  436. my $replaceString = eval( 'my $tmp = "' . $replace . '";' );
  437. if ( swiMatchPatternCount( $matchStr, '\n' ) !=
  438. swiMatchPatternCount( $replaceString, '\n' ) )
  439. {
  440. die(
  441. "Wrong preprocessor rule detected in the configuration file: it changes number of lines replacing the text\n>>>\n"
  442. . $matchStr
  443. . "\n<<<\nby\n>>>\n"
  444. . $replaceString
  445. . "\n<<<\n" );
  446. }
  447. my $blockFrag =
  448. substr( $code, $posPrev, $posCurrent - $posPrev - length($matchStr) );
  449. $result .= ( $blockFrag . $replaceString );
  450. }
  451. $result .= $lastPart;
  452. return $result;
  453. }
  454. sub swiSourceCodeScan
  455. {
  456. my $file = shift();
  457. my $offset = shift();
  458. my $code = shift();
  459. my $search = shift();
  460. my $message = shift();
  461. my @result = [];
  462. while ( $code =~ m/$search/g )
  463. {
  464. my $matchPre = $`;
  465. my $matchStr = $&;
  466. my $linePos = swiMatchPatternCount( $matchPre . $matchStr, '\n' ) + 1;
  467. my $messageString = eval( 'my $tmp = "' . $message . '";' );
  468. push(@result, {
  469. 'swi:ref:type' => 'scan',
  470. 'swi:scan:file' => $file,
  471. 'swi:scan:line' => $linePos + $offset,
  472. 'swi:scan:message' => $messageString
  473. });
  474. }
  475. return @result;
  476. }
  477. sub swiSourceCodeGlobalGet
  478. {
  479. my $functionsData = shift();
  480. my $endLine = shift();
  481. my @blockCodeAgr = @_;
  482. my @blockCode;
  483. if ( $#blockCodeAgr == 0 )
  484. {
  485. # Continues code string provided
  486. @blockCode = split( "\n", $blockCodeAgr[0] );
  487. }
  488. else
  489. {
  490. # Array of lines provided
  491. @blockCode = @blockCodeAgr;
  492. }
  493. if ( !defined($endLine) )
  494. {
  495. $endLine = $#blockCode + 100; # Just with overhead
  496. }
  497. foreach my $function ( values %{$functionsData} )
  498. {
  499. for (
  500. my $i = $function->{'swi:line:commentstart'} ;
  501. $i <= $function->{'swi:line:blockend'} ;
  502. $i++
  503. )
  504. {
  505. # Clear line if it is inside function
  506. $blockCode[$i] = "";
  507. }
  508. }
  509. my $result = "";
  510. foreach (@blockCode)
  511. {
  512. $endLine--;
  513. if ( $endLine < 0 )
  514. {
  515. last;
  516. }
  517. $result .= $_ . "\n";
  518. }
  519. return $result;
  520. }
  521. sub swiDupindexFunctionAdd
  522. {
  523. my $fileName = shift();
  524. my $functionName = shift();
  525. my $functionCode = shift();
  526. print $dupindexIn "init_file\n";
  527. print $dupindexIn length($fileName) . "\n";
  528. print $dupindexIn length($functionName) . "\n";
  529. print $dupindexIn length($functionCode) . "\n";
  530. print $dupindexIn $fileName . $functionName . $functionCode . "\n";
  531. }
  532. sub swiSourceIndexAdd
  533. {
  534. my $file = shift();
  535. my $functionsData = shift();
  536. my $scanerRules = shift();
  537. my $dupfinderSettings = shift();
  538. my @block_Initial = split( "\n", shift() );
  539. my @block_Code = split( "\n", shift() );
  540. my @block_Comment = split( "\n", shift() );
  541. my @block_NoPrep = split( "\n", shift() );
  542. my @block_NoStr = split( "\n", shift() );
  543. my $block_Purified = shift();
  544. # Extend arrays in order to remove warnings
  545. my $finalLinesNum = swiMatchPatternCount( $block_Purified, '\n' ) + 1;
  546. @block_Initial = swiUtilArrayExtend( [@block_Initial], $finalLinesNum, "" );
  547. @block_Code = swiUtilArrayExtend( [@block_Code], $finalLinesNum, "" );
  548. @block_Comment = swiUtilArrayExtend( [@block_Comment], $finalLinesNum, "" );
  549. @block_NoPrep = swiUtilArrayExtend( [@block_NoPrep], $finalLinesNum, "" );
  550. @block_NoStr = swiUtilArrayExtend( [@block_NoStr], $finalLinesNum, "" );
  551. foreach my $functionName ( keys %{$functionsData} )
  552. {
  553. my $function = $functionsData->{$functionName};
  554. my $block = {};
  555. $block->{'functionname'} = $functionName;
  556. $block->{'functionname'} =~
  557. s/($regexpCodeContainerDelimeter)?($regexpCodeContainerIdentifier$regexpCodeContainerDelimeter)*($regexpCodeFunctionIdentifier)/$3/;
  558. if ( $functionName ne $regexpCodeGlobalFunctionName )
  559. {
  560. # Get content
  561. $block->{'initial'} = "";
  562. for (
  563. my $i = $function->{'swi:line:commentstart'} ;
  564. $i <= $function->{'swi:line:blockend'} ;
  565. $i++
  566. )
  567. {
  568. $block->{'initial'} .= $block_Initial[$i] . "\n";
  569. }
  570. $block->{'code'} = "";
  571. for (
  572. my $i = $function->{'swi:line:commentstart'} ;
  573. $i <= $function->{'swi:line:blockend'} ;
  574. $i++
  575. )
  576. {
  577. $block->{'code'} .= $block_Code[$i] . "\n";
  578. }
  579. $block->{'comments'} = "";
  580. for (
  581. my $i = $function->{'swi:line:commentstart'} ;
  582. $i <= $function->{'swi:line:blockend'} ;
  583. $i++
  584. )
  585. {
  586. $block->{'comments'} .= $block_Comment[$i] . "\n";
  587. }
  588. $block->{'nopreprocessor'} = "";
  589. for (
  590. my $i = $function->{'swi:line:commentstart'} ;
  591. $i <= $function->{'swi:line:blockend'} ;
  592. $i++
  593. )
  594. {
  595. $block->{'nopreprocessor'} .= $block_NoPrep[$i] . "\n";
  596. }
  597. $block->{'nostrings'} = "";
  598. for (
  599. my $i = $function->{'swi:line:commentstart'} ;
  600. $i <= $function->{'swi:line:blockend'} ;
  601. $i++
  602. )
  603. {
  604. $block->{'nostrings'} .= $block_NoStr[$i] . "\n";
  605. }
  606. $block->{'purified'} = substr(
  607. $block_Purified,
  608. $function->{'swi:pos:headerstart'},
  609. $function->{'swi:pos:blockend'} -
  610. $function->{'swi:pos:headerstart'}
  611. )
  612. . "\n";
  613. $block->{'commentshead'} = "";
  614. for (
  615. my $i = $function->{'swi:line:commentstart'} ;
  616. $i < $function->{'swi:line:headerstart'} ;
  617. $i++
  618. )
  619. {
  620. $block->{'commentshead'} .= $block_Comment[$i] . "\n";
  621. }
  622. # functionname created above
  623. $block->{'functionhead'} = substr(
  624. $block_Purified,
  625. $function->{'swi:pos:headerstart'},
  626. $function->{'swi:pos:blockstart'} -
  627. $function->{'swi:pos:headerstart'}
  628. )
  629. . "\n";
  630. $block->{'functionbody'} = substr(
  631. $block_Purified,
  632. $function->{'swi:pos:blockstart'},
  633. $function->{'swi:pos:blockend'} -
  634. $function->{'swi:pos:blockstart'}
  635. )
  636. . "\n";
  637. }
  638. else
  639. {
  640. $block->{'initial'} =
  641. swiSourceCodeGlobalGet( $functionsData, undef, @block_Initial );
  642. $block->{'code'} =
  643. swiSourceCodeGlobalGet( $functionsData, undef, @block_Code );
  644. $block->{'comments'} =
  645. swiSourceCodeGlobalGet( $functionsData, undef, @block_Comment );
  646. $block->{'nopreprocessor'} =
  647. swiSourceCodeGlobalGet( $functionsData, undef, @block_NoPrep );
  648. $block->{'nostrings'} =
  649. swiSourceCodeGlobalGet( $functionsData, undef, @block_NoStr );
  650. $block->{'purified'} =
  651. swiSourceCodeGlobalGet( $functionsData, undef, $block_Purified );
  652. $block->{'commentshead'} =
  653. swiSourceCodeGlobalGet( $functionsData,
  654. $function->{'swi:line:headerstart'},
  655. @block_Comment );
  656. }
  657. # Initialize container
  658. $function->{'swi:statistic'} = {
  659. 'swi:length' => {
  660. 'swi:source' => {},
  661. 'swi:executable' => {},
  662. 'swi:comment' => {},
  663. 'swi:blank' => {},
  664. 'swi:function:name' => {}
  665. },
  666. 'swi:lines' => {
  667. 'swi:source' => {},
  668. 'swi:executable' => {},
  669. 'swi:comment' => {},
  670. 'swi:blank' => {},
  671. 'swi:comment:header' => {}
  672. },
  673. 'swi:complexity' => {
  674. 'swi:cyclomatic' => {},
  675. 'swi:blocks' => {},
  676. 'swi:maxdepth' => {}
  677. },
  678. 'swi:duplication' => { 'swi:symbols' => { 'swi:exact' => 0 }, },
  679. 'swi:checksum' => { 'swi:source' => { 'swi:exact' => 0 }, }
  680. };
  681. $function->{'swi:reference'} = [];
  682. # Calculate swi:length->swi:source
  683. $function->{'swi:statistic'}->{'swi:length'}->{'swi:source'}
  684. ->{'swi:exact'} = length( $block->{'initial'} );
  685. # Calculate swi:length->swi:executable
  686. $function->{'swi:statistic'}->{'swi:length'}->{'swi:executable'}
  687. ->{'swi:exact'} = length( $block->{'purified'} );
  688. # Calculate swi:length->swi:comment
  689. $function->{'swi:statistic'}->{'swi:length'}->{'swi:comment'}
  690. ->{'swi:exact'} = length( $block->{'comments'} );
  691. # Calculate swi:length->swi:blank
  692. $function->{'swi:statistic'}->{'swi:length'}->{'swi:blank'}
  693. ->{'swi:exact'} = swiMatchPatternCount( $block->{'initial'}, '\s' );
  694. # Calculate swi:length->swi:function-name
  695. $function->{'swi:statistic'}->{'swi:length'}->{'swi:function:name'}
  696. ->{'swi:exact'} = length( $block->{'functionname'} );
  697. # Calculate swi:lines->swi:source
  698. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:source'}
  699. ->{'swi:exact'} = swiMatchPatternCount( $block->{'initial'}, '\n' );
  700. # Calculate swi:lines->swi:executable
  701. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:executable'}
  702. ->{'swi:exact'} =
  703. swiMatchLinesCount( $block->{'purified'}, '[^ \t]' );
  704. # Calculate swi:lines->swi:comment
  705. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:comment'}
  706. ->{'swi:exact'} =
  707. swiMatchLinesCount( $block->{'comments'}, '[^ \t]' );
  708. # Calculate swi:lines->swi:blank
  709. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:blank'}
  710. ->{'swi:exact'} = swiMatchLinesCount( $block->{'initial'}, '^\s*$' );
  711. # Calculate swi:lines->swi:comment:header
  712. $function->{'swi:statistic'}->{'swi:lines'}->{'swi:comment:header'}
  713. ->{'swi:exact'} =
  714. $function->{'swi:line:headerstart'} -
  715. $function->{'swi:line:commentstart'};
  716. # Calculate swi:complexity->swi:cyclomatic
  717. $function->{'swi:statistic'}->{'swi:complexity'}->{'swi:cyclomatic'}
  718. ->{'swi:exact'} =
  719. swiMatchPatternCount(
  720. swiSourceCodeStringsRemove( $block->{'purified'} ),
  721. $regexpCodeMatch_ComplexityCyclomatic );
  722. # Calculate swi:complexity->swi:blocks
  723. # -1 in order not to count main function's block
  724. $function->{'swi:statistic'}->{'swi:complexity'}->{'swi:blocks'}
  725. ->{'swi:exact'} =
  726. swiMatchPatternCount( $block->{'purified'}, $regexpCodeBlockStart ) -
  727. 1;
  728. # Calculate swi:complexity->swi:maxdepth
  729. $function->{'swi:statistic'}->{'swi:complexity'}->{'swi:maxdepth'}
  730. ->{'swi:exact'} = $function->{'swi:depth'};
  731. # Calculate swi:checksum->swi:index
  732. my @symbols = split( //, $block->{'initial'} );
  733. my $crcSumTotal = 0;
  734. for ( my $pos = 0 ; $pos <= $#symbols ; $pos++ )
  735. {
  736. $crcSumTotal =
  737. ( $crcSumTotal << 8 )
  738. ^ $crctab[ ( $crcSumTotal >> 24 ) ^ ( unpack 'C', $symbols[$pos] )
  739. ];
  740. }
  741. $function->{'swi:statistic'}->{'swi:checksum'}->{'swi:source'}
  742. ->{'swi:exact'} = $crcSumTotal;
  743. # Scan function by scaner tool
  744. foreach my $rule ( @{$scanerRules} )
  745. {
  746. if ( defined( $block->{ $rule->{'swi:codecontent'} } ) )
  747. {
  748. push(
  749. @{ $function->{'swi:reference'} },
  750. swiSourceCodeScan(
  751. $file, $function->{'swi:line:headerstart'},
  752. $block->{ $rule->{'swi:codecontent'} },
  753. $rule->{'swi:searchpattern'},
  754. $rule->{'swi:messagepattern'}
  755. )
  756. );
  757. }
  758. else
  759. {
  760. STATUS(
  761. "Wrong configuiration: 'swi:scaner/swi:rule/swi:codecontent' property is not from the acceptable set of values!"
  762. );
  763. $exitCode++;
  764. }
  765. }
  766. # Add to dupindexer for further processing
  767. if ( defined( $block->{ $dupfinderSettings->{'swi:codecontent'} } ) )
  768. {
  769. if (
  770. $functionName ne $regexpCodeGlobalFunctionName
  771. || ( defined( $dupfinderSettings->{'swi:globalcode'} )
  772. && $dupfinderSettings->{'swi:globalcode'} eq 'on' )
  773. )
  774. {
  775. swiDupindexFunctionAdd( $file, $functionName,
  776. $block->{ $dupfinderSettings->{'swi:codecontent'} } );
  777. }
  778. }
  779. else
  780. {
  781. STATUS(
  782. "Wrong configuiration: 'swi:indexer:dup/swi:codecontent' property is not from the acceptable set of values!"
  783. );
  784. $exitCode++;
  785. }
  786. }
  787. return $functionsData;
  788. }
  789. #
  790. # This function uses external tool for duplication collection
  791. #
  792. sub swiSourceIndexDuplicationAdd
  793. {
  794. my $filesData = shift();
  795. STATUS("Searching for duplication...");
  796. print $dupindexIn "start\n";
  797. my $outStream = "";
  798. while ( defined($dupindexOut) && ( my $line = <$dupindexOut> ) )
  799. {
  800. $outStream .= $line;
  801. }
  802. my @dupdataGoups = split( /info: group_start/, $outStream );
  803. foreach (@dupdataGoups)
  804. {
  805. my @parsedData;
  806. my @dupdataSplit = split( /\n/, $_ );
  807. foreach (@dupdataSplit)
  808. {
  809. if ( $_ =~
  810. m/duplication: file: '(.*)' function: '(.*)' possition: '(.*)' size: '(.*)'/
  811. )
  812. {
  813. $filesData->{$1}->{$2}->{'swi:statistic'}->{'swi:duplication'}
  814. ->{'swi:symbols'}->{'swi:exact'} += $4;
  815. push(
  816. @parsedData,
  817. {
  818. 'swi:ref:type' => 'dup',
  819. 'swi:dup:file' => $1,
  820. 'swi:dup:function' => $2,
  821. 'swi:dup:line' => $3 +
  822. $filesData->{$1}->{$2}->{'swi:line:headerstart'},
  823. 'swi:dup:size' => $4
  824. }
  825. );
  826. }
  827. }
  828. foreach my $dupData (@parsedData)
  829. {
  830. push(
  831. @{
  832. $filesData->{ $dupData->{'swi:dup:file'} }
  833. ->{ $dupData->{'swi:dup:function'} }->{'swi:reference'}
  834. },
  835. @parsedData
  836. );
  837. }
  838. }
  839. my $errStream = "";
  840. while ( defined($dupindexErr) && ( my $line = <$dupindexErr> ) )
  841. {
  842. STATUS("Internal dupindex tool detected the error.");
  843. STATUS($line);
  844. STATUS(" 1. Check 'swi:indexer:dup' section in configuration.");
  845. STATUS(
  846. " 2. Check that swi/dupindex tool is runable on your system/platform."
  847. );
  848. STATUS(" 3. Recompile swi/dupindex tool for your system/platform.");
  849. STATUS(" 4. Report the problem to developers.");
  850. # The final return code is unsuccessful
  851. # It means that these errors/warnings should be fixed before the next processing
  852. $exitCode++;
  853. }
  854. return $filesData;
  855. }
  856. sub swiSourceCodeParse
  857. {
  858. my $file = shift();
  859. my $blockCode = shift();
  860. my @blockComment = split( "\n", shift() );
  861. @blockComment =
  862. swiUtilArrayExtend( [@blockComment],
  863. swiMatchPatternCount( $blockCode, '\n' ) + 1, "" );
  864. my $result = {};
  865. my $deepLevel = 0;
  866. if ( swiMatchPatternCount( $blockCode, $regexpCodeBlockStart ) !=
  867. swiMatchPatternCount( $blockCode, $regexpCodeBlockEnd ) )
  868. {
  869. PRINT( $file, 0, "error",
  870. "Mismatch in number of start/end of blocks delimeters!\n" );
  871. DEBUG("The code before parsing:\n$blockCode");
  872. return $result;
  873. }
  874. my $posPrev = 0;
  875. my $posCurrent = 0;
  876. my $isInFunction = 0;
  877. my $currentFunc = undef;
  878. my $currentLine = 0;
  879. my $currentPos = 0;
  880. my $currentDepth = 0;
  881. my @containers;
  882. while ( $blockCode =~ m/($regexpCodeBlockStart)|($regexpCodeBlockEnd)/g )
  883. {
  884. $posPrev = $posCurrent;
  885. $posCurrent = pos($blockCode);
  886. my $matchPre = $`;
  887. my $matchStr = $&;
  888. my $matchPost = $';
  889. my $blockFragFull =
  890. substr( $blockCode, $posPrev, $posCurrent - $posPrev );
  891. $currentLine += swiMatchPatternCount( $blockFragFull, "\n" );
  892. $currentPos += length($blockFragFull);
  893. if ( $matchStr =~ m/^$regexpCodeBlockStart$/ )
  894. {
  895. my $blockFragLast = $blockFragFull;
  896. $blockFragLast =~ s/^($regexpCodeStatements)*\s*//;
  897. # opened block detected
  898. $deepLevel++;
  899. if ( $isInFunction == 0 )
  900. {
  901. # Check whether it is a function start
  902. if ( $blockFragLast =~
  903. m/^($regexpCodeFunctionModifier)*($regexpCodeFunctionIdentifier)($regexpCodeFunctionArguments)($regexpCodeBlockStart)$/
  904. )
  905. {
  906. my $matchedHeader = $&;
  907. my $mod = $1;
  908. my $word = $2;
  909. if ( !defined($mod) )
  910. {
  911. $mod = "";
  912. }
  913. # Remove empty symbols in header
  914. $matchedHeader =~ s/^\s*//;
  915. # Check if modifier consumes something from identifier
  916. # and return back the last part from modifier to identifier
  917. ( $mod . $word ) =~ m/$regexpCodeFunctionIdentifier$/;
  918. $mod = $`;
  919. $word = $&;
  920. # Remove empty symbols in indetifier
  921. $word =~ s/[\n\t ]+/ /g;
  922. # Remove empty symbols in modifier
  923. $mod =~ s/^\s*//;
  924. $mod =~ s/\s*$//;
  925. $mod =~ s/[\n\t ]+/ /g;
  926. if ( $word !~ m/^($regexpCodeKeyword)$/ )
  927. {
  928. $isInFunction = $deepLevel;
  929. # Attach containers to the name of function
  930. foreach my $container ( reverse @containers )
  931. {
  932. if ( defined($container) )
  933. {
  934. $word = $container
  935. . $regexpCodeContainerDelimeter
  936. . $word;
  937. }
  938. }
  939. # Add the code before the block start
  940. # (name of function, arguments, ..)
  941. my $matchedHeaderLinesNum =
  942. swiMatchPatternCount( $matchedHeader, "\n" );
  943. # Check if there are comments before the header
  944. my $consumedTopLines = 1;
  945. while (
  946. $currentLine - $matchedHeaderLinesNum -
  947. $consumedTopLines >= 0
  948. && $blockComment[
  949. $currentLine - $matchedHeaderLinesNum -
  950. $consumedTopLines
  951. ] !~ m/^\s*$/
  952. )
  953. {
  954. $consumedTopLines++;
  955. }
  956. if ( defined( $result->{$word} ) )
  957. {
  958. PRINT( $file, $currentLine + 1, 'debug',
  959. "The same function detected more than once: '$word'"
  960. );
  961. my $counter = 1;
  962. while (
  963. defined( $result->{ $word . ":" . $counter } ) )
  964. {
  965. $counter++;
  966. }
  967. $word .= ":" . $counter;
  968. }
  969. # Store data in result
  970. $currentFunc = $word;
  971. $result->{$word} = {
  972. 'swi:pos:blockstart' => $currentPos,
  973. 'swi:pos:headerstart' => $currentPos -
  974. length($matchedHeader),
  975. 'swi:pos:blockend' => undef,
  976. 'swi:line:blockstart' => $currentLine,
  977. 'swi:line:headerstart' => $currentLine -
  978. $matchedHeaderLinesNum,
  979. 'swi:line:commentstart' => $currentLine -
  980. $matchedHeaderLinesNum -
  981. ( $consumedTopLines - 1 ),
  982. 'swi:line:blockend' => undef,
  983. 'swi:modifier' => $mod,
  984. 'swi:indent' => $deepLevel
  985. };
  986. PRINT( $file, $currentLine + 1, 'debug',
  987. "Function detected: '$word', with modifier: '$mod'"
  988. );
  989. }
  990. }
  991. # Check whether it is a container start
  992. elsif ( $blockFragLast =~
  993. m/^($regexpCodeContainerModifier)\s+($regexpCodeContainerIdentifier)($regexpCodeContainerArguments)($regexpCodeBlockStart)$/
  994. )
  995. {
  996. my $mod = $1;
  997. my $word = $2;
  998. $mod =~ s/^\s*//;
  999. $mod =~ s/\s*$//;
  1000. $mod =~ s/[\n\t ]+/ /g;
  1001. if ( $word !~ m/^$regexpCodeKeyword$/ )
  1002. {
  1003. PRINT( $file, $currentLine + 1, 'debug',
  1004. "Container detected: '$word', with modifier: '$mod'"
  1005. );
  1006. $containers[ $deepLevel - 1 ] = $word;
  1007. }
  1008. }
  1009. # Check if block start should be ignored
  1010. elsif ( $blockFragLast =~
  1011. m/($regexpCodeBlockStartIgnore)($regexpCodeBlockStart)$/ )
  1012. {
  1013. PRINT( $file, $currentLine + 1,
  1014. 'debug', "Block start delimeter ignored/missed: '$&'" );
  1015. }
  1016. elsif ( $blockFragLast =~ m/^\s*($regexpCodeBlockStart)$/ )
  1017. {
  1018. PRINT( $file, $currentLine + 1, 'debug',
  1019. "Empty code before block start delimeter detected: '$&'"
  1020. );
  1021. }
  1022. else
  1023. {
  1024. $blockFragLast =~ s/^\s*//;
  1025. PRINT( $file, $currentLine + 1,
  1026. 'error',
  1027. "Unknown type of started block: '$blockFragLast'" );
  1028. $exitCode++;
  1029. }
  1030. }
  1031. else
  1032. {
  1033. # Calculate maximum indent level
  1034. if ( $currentDepth < $deepLevel - $isInFunction )
  1035. {
  1036. $currentDepth = $deepLevel - $isInFunction;
  1037. }
  1038. }
  1039. }
  1040. else
  1041. {
  1042. # block closer detected
  1043. if ( $deepLevel == $isInFunction )
  1044. {
  1045. $isInFunction = 0;
  1046. $result->{$currentFunc}->{'swi:pos:blockend'} = $currentPos;
  1047. $result->{$currentFunc}->{'swi:line:blockend'} = $currentLine;
  1048. $result->{$currentFunc}->{'swi:depth'} = $currentDepth;
  1049. $currentDepth = 0;
  1050. $currentFunc = undef;
  1051. }
  1052. $containers[ $deepLevel - 1 ] = undef;
  1053. $deepLevel--;
  1054. }
  1055. }
  1056. # Initialize GLOBAL function
  1057. $result->{$regexpCodeGlobalFunctionName} = {
  1058. 'swi:pos:blockstart' => 0, # not used
  1059. 'swi:pos:headerstart' => 0, # not used
  1060. 'swi:pos:blockend' => 0, # not used,
  1061. 'swi:line:blockstart' => 0, # not used
  1062. 'swi:line:headerstart' => 0, # used to deattach header with comments
  1063. 'swi:line:commentstart' => 0, # not used
  1064. 'swi:line:blockend' => 0, # not used
  1065. 'swi:modifier' => "", # not used
  1066. 'swi:indent' => 0, # not used
  1067. 'swi:depth' => 0 # not used
  1068. };
  1069. for ( my $i = 0 ; $i <= $#blockComment ; ++$i )
  1070. {
  1071. $result->{$regexpCodeGlobalFunctionName}->{'swi:line:headerstart'} = $i;
  1072. if ( $blockComment[$i] =~ m/^\s*$/ )
  1073. {
  1074. # Empty line detected => header end matched
  1075. last;
  1076. }
  1077. }
  1078. return $result;
  1079. }
  1080. sub swiSourceCodeStringsRemove
  1081. {
  1082. my $code = shift();
  1083. my $result = "";
  1084. my $posPrev = 0;
  1085. my $posCurrent = 0;
  1086. my $isInString = 0;
  1087. my $lastPart = $code;
  1088. my $borderPatternDynamic = $regexpCodeStringBorder_Text;
  1089. while ( $code =~ m/$borderPatternDynamic/g )
  1090. {
  1091. $posPrev = $posCurrent;
  1092. $posCurrent = pos($code);
  1093. my $matchPre = $`;
  1094. my $matchStr = $&;
  1095. my $matchPost = $';
  1096. $lastPart = "";
  1097. my $blockFrag =
  1098. substr( $code, $posPrev, $posCurrent - $posPrev - length($matchStr) );
  1099. if ( $isInString == 0 )
  1100. {
  1101. $result .= $blockFrag . $matchStr;
  1102. $isInString = 1;
  1103. $borderPatternDynamic = $matchStr;
  1104. }
  1105. else
  1106. {
  1107. if ( ( $blockFrag . $matchStr ) =~
  1108. m/($regexpCodeStringBorder_Escape)($borderPatternDynamic)$/ )
  1109. {
  1110. # This string border should be ignored
  1111. $blockFrag =~ s/[^\n]//g;
  1112. $result .= $blockFrag;
  1113. }
  1114. else
  1115. {
  1116. # Save last part only if string end detected;
  1117. $lastPart = $matchPost;
  1118. # Keep newlines in the code inside strings
  1119. $blockFrag =~ s/[^\n]//g;
  1120. $result .= $blockFrag;
  1121. $result .= $matchStr;
  1122. $isInString = 0;
  1123. $borderPatternDynamic = $regexpCodeStringBorder_Text;
  1124. }
  1125. }
  1126. }
  1127. return $result . $lastPart;
  1128. }
  1129. sub swiSourceCodePreprocessorRemove
  1130. {
  1131. my $code = shift();
  1132. my $result = "";
  1133. my @codeLines = swiUtilArrayExtend( [ split( "\n", $code ) ],
  1134. swiMatchPatternCount( $code, '\n' ) - 1, "" );
  1135. my $isInPreprocessor = 0;
  1136. foreach my $line (@codeLines)
  1137. {
  1138. if ( $isInPreprocessor == 1
  1139. || $line =~ m/$regexpCodePreprocessorStart/ )
  1140. {
  1141. $isInPreprocessor = 1;
  1142. # Just add empty line
  1143. $result .= "\n";
  1144. if ( $line =~ m/$regexpCodePreprocessorEnd/ )
  1145. {
  1146. $isInPreprocessor = 0;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. $line =~ s/\n//g;
  1152. $result .= $line . "\n";
  1153. }
  1154. }
  1155. return $result;
  1156. }
  1157. sub swiSourceCommentsDeattach
  1158. {
  1159. my @fileLines = @_;
  1160. my $blockAll = "";
  1161. my $blockCode = "";
  1162. my $blockComment = "";
  1163. my $countLinesAll = 0;
  1164. my $countLinesCode = 0;
  1165. my $countLinesComment = 0;
  1166. my $isInCode = 1;
  1167. foreach my $line (@fileLines)
  1168. {
  1169. if ( $isInCode == 1 )
  1170. {
  1171. if ( $line =~
  1172. m/(($regexpCommentMultiStart)|($regexpCommentSingle))/ )
  1173. {
  1174. my $matchPre = $`;
  1175. my $matchStr = $&;
  1176. my $matchPost = $';
  1177. my $marker = "#@%" . int( rand(0xFF) ) . "%@#";
  1178. if ( swiSourceCodeStringsRemove( $matchPre . $marker ) !~
  1179. m/$marker/ )
  1180. {
  1181. # This block ends inside the string
  1182. # i.e. comment start marker detected inside the string
  1183. # Process again the string with removed comment start marker
  1184. $line =~
  1185. s/(($regexpCommentMultiStart)|($regexpCommentSingle))//;
  1186. $fileLines[$countLinesAll] = $line;
  1187. redo;
  1188. }
  1189. elsif ( $matchStr =~ m/^$regexpCommentMultiStart$/ )
  1190. {
  1191. # Get the code before the comment
  1192. $blockCode .= $matchPre;
  1193. $blockAll .= $matchPre;
  1194. $isInCode = 0;
  1195. $blockComment .= $matchStr;
  1196. $blockAll .= $matchStr;
  1197. # Process again the last fragment
  1198. $fileLines[$countLinesAll] = $matchPost;
  1199. $line = $matchPost;
  1200. redo;
  1201. }
  1202. else
  1203. {
  1204. # Get the code before the comment
  1205. $blockCode .= $matchPre;
  1206. $blockAll .= $matchPre;
  1207. # Single line comment detected
  1208. $matchPost =~ s/\n//g;
  1209. $blockComment .= $matchStr . $matchPost;
  1210. $blockAll .= $matchStr . $matchPost;
  1211. }
  1212. }
  1213. else
  1214. {
  1215. $line =~ s/\n//g;
  1216. $blockCode .= $line;
  1217. $blockAll .= $line;
  1218. }
  1219. }
  1220. else
  1221. {
  1222. # Check multi line comment end
  1223. if ( $line =~ m/$regexpCommentMultiEnd/ )
  1224. {
  1225. my $matchPre = $`;
  1226. my $matchStr = $&;
  1227. my $matchPost = $';
  1228. # Get the code before the comment
  1229. $blockComment .= $matchPre . $matchStr;
  1230. $blockAll .= $matchPre . $matchStr;
  1231. $isInCode = 1;
  1232. # Process again the last fragment
  1233. $fileLines[$countLinesAll] = $matchPost;
  1234. $line = $matchPost;
  1235. redo;
  1236. }
  1237. else
  1238. {
  1239. $line =~ s/\n//g;
  1240. $blockComment .= $line;
  1241. $blockAll .= $line;
  1242. }
  1243. }
  1244. $blockCode .= "\n";
  1245. $blockComment .= "\n";
  1246. $blockAll .= "\n";
  1247. $countLinesAll++;
  1248. }
  1249. return ( $blockAll, $blockCode, $blockComment );
  1250. }
  1251. sub swiMatchPatternCount
  1252. {
  1253. my $text = shift();
  1254. my $pattern = shift();
  1255. my $count = 0;
  1256. while ( $text =~ m/$pattern/g )
  1257. {
  1258. $count++;
  1259. }
  1260. return $count;
  1261. }
  1262. sub swiMatchLinesCount
  1263. {
  1264. my @text = split( "\n", shift() );
  1265. my $pattern = shift();
  1266. my $count = 0;
  1267. foreach (@text)
  1268. {
  1269. if ( $_ =~ m/$pattern/ )
  1270. {
  1271. $count++;
  1272. }
  1273. }
  1274. return $count;
  1275. }
  1276. sub swiUtilArrayExtend
  1277. {
  1278. my $array = shift();
  1279. my $finalLength = shift();
  1280. my $emptySymbol = shift();
  1281. if ( $#{$array} > $finalLength )
  1282. {
  1283. DEBUG( "Length of array: " . $#{$array} );
  1284. DEBUG( "Requested final length: " . $finalLength );
  1285. DEBUG( Dumper($array) );
  1286. die('Internal Error occured!');
  1287. }
  1288. while ( $finalLength - $#{$array} > 0 )
  1289. {
  1290. push( @{$array}, $emptySymbol );
  1291. }
  1292. return @{$array};
  1293. }
  1294. sub swiUtilDirectoryCleanUp
  1295. {
  1296. my $dir = shift();
  1297. die("Internal error!") if not defined $dir;
  1298. die("Internal error!") if $dir eq "";
  1299. die("Internal error!") if $dir eq ".";
  1300. die("Internal error!") if $dir eq "./";
  1301. if ( opendir( DIR, $dir ) )
  1302. {
  1303. while ( my $file = readdir(DIR) )
  1304. {
  1305. unlink $dir . "/" . $file;
  1306. }
  1307. closedir(DIR);
  1308. }
  1309. }
  1310. return 1;