Processor.pm 51 KB

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