Processor.pm 58 KB

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