Processor.pm 56 KB

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