blib/lib/C/Analyzer.pm

filter: Statementstmt 13.9% 41 / 293 Branchbran 8.0% 8 / 100 Conditioncond 0.0% 0 / 91 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 33.3% 6 / 18 Podpod 100% 13 / 13 totaltotal 12.2% 68 / 557 CC 89 SCAR 54.9
CC
89
Cov
10%
parseCFile
650.0
updateHashTable
240.0
generateCalltree
210.0
CRAP 242.9
↑ Summary
1 #!/usr/bin/perl
2 package C::Analyzer;
3  
4 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '0.01';
8 1 @ISA = qw(Exporter);
9  
10 #Give a hoot don't pollute, do not export more than needed by default
11 1 @EXPORT = qw();
12 1 @EXPORT_OK = qw();
13 1 %EXPORT_TAGS = ();
14 }
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 my %calls = ();
20 1 my @calls_table = ();
21 1 my @rec_track = ();
22  
23 #################### subroutine header begin ####################
24  
25 =head2 new
26  
27 Usage : my $analyzer = new Analyzer(
28 _inputPath => "/home/sindhu/test/afs", # folder path
29 _cppPath => "/usr/bin", # GNU C preprocessor path
30 _inputOption => "dir_and_subdir", # if dir or dir/subdir
31 );
32 Purpose : Constructors, Static method that return an object
33 Returns : Object
34 Argument : _inputPath, _inputOption, _cppPath, _functionName
35 Throws : None
36 Comment : Can be extended for other important inputs as well in future.
37 See Also :
38  
39 =cut
40  
41 #################### subroutine header end ####################
42  
43 1 ▶sub new {
Subroutine new: called CC=9 SCAR=26.3
Pod documented
44 1 my $class = shift;
45 1 my %params = @_;
46 1 my $self = {};
47 1 ▶ if ( defined( $params{'_inputPath'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_inputPath'}) { } 1 0 1
48 1 $self->{'_inputPath'} = $params{'_inputPath'};
49 }
50 else {
51 0 print "Error: Missing file/folder path\n";
52 0 exit;
53 }
54  
55 1 ▶ if ( defined( $params{'_inputOption'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_inputOption'}) { } 1 0 1
56 1 $self->{'_inputOption'} = $params{'_inputOption'};
57 }
58 else {
59 0 init $self->{'_inputOption'} = "dir";
60 }
61 1 ▶ if ( defined( $params{'_cppPath'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_cppPath'}) { } 1 0 1
62 1 $self->{'_cppPath'} = $params{'_cppPath'};
63 }
64 else {
65 0 print "Error: Missing GNU C Processor path\n";
66 0 exit;
67 }
68 1 ▶ if ( defined( $params{'_cppOptions'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_cppOptions'}) { } 0 1 1
69 0 $self->{'_cppOptions'} = $params{'_cppOptions'};
70 }
71 else {
72 1 $self->{'_cppOptions'} = "-nostdinc";
73 }
74 1 ▶ if ( defined( $params{'_functionName'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_functionName'}) { } 0 1 1
75 0 $self->{'_functionName'} = $params{'_functionName'};
76 }
77 else {
78 1 $self->{'_functionName'} = "main";
79 }
80 1 ▶ if ( defined( $params{'_reportType'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_reportType'}) { } 0 1 1
81 0 $self->{'_reportType'} = $params{'_reportType'};
82 }
83 else {
84 1 $self->{'_reportType'} = "Text";
85 }
86 1 ▶ if ( defined( $params{'_reportOptions'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_reportOptions'}) { } 0 1 1
87 0 $self->{'_reportOptions'} = $params{'_reportOptions'};
88 }
89 else {
90 1 $self->{'_reportOptions'} = "fullDetails";
91 }
92 1 ▶ if ( defined( $params{'_treeType'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{'_treeType'}) { } 0 1 1
93 0 $self->{'_treeType'} = $params{'_treeType'};
94 }
95 else {
96 1 $self->{'_treeType'} = "callTree";
97 }
98 1 bless $self, $class;
99 }
100  
101 #################### subroutine header begin ####################
102  
103 =head2 init
104  
105 Usage : init()
106 Purpose : initializes variables, gets C files in dir/subdirs,
107 runs GNU C Preprocessor and updates functions and
108 calls in each C file
109 Returns : None
110 Argument : None
111 Throws : None
112 Comment : None
113 See Also :
114  
115 =cut
116  
117 #################### subroutine header end ####################
118  
119 0 ▶sub init() {
Subroutine init: not called CC=5 SCAR=34.0
Pod documented
120 0 my ($self) = @_;
121 0 &clean();
122 0 my $folder = "";
123 0 my $opt = "";
124 0 my $cppPath = "";
125 0 my $cppOpts = "";
126 0 my $funName = "";
127 0 my $cfiles;
128 my $ppfiles;
129 0 my $cfile;
130  
131 0 ▶ $folder = $self->{_inputPath} if defined($folder);
Branch: 1 branch
BranchTrueFalseTotal
if defined $folder 0 0 0
132 0 ▶ $opt = $self->{_inputOption} if defined($opt);
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt 0 0 0
133 0 ▶ $cppPath = $self->{_cppPath} if defined($cppPath);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cppPath 0 0 0
134 0 ▶ $cppOpts = $self->{_cppOptions} if defined($cppOpts);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cppOpts 0 0 0
135  
136 0 $cfiles = &getListOfCFiles( \$folder, \$opt );
137 0 $ppfiles = &runGnuPreprocessor( \$cfiles, \$cppPath, \$cppOpts );
138 0 &identifyFunctionsAndCalls( \$ppfiles, \$opt, \$folder );
139 0 return;
140 }
141  
142 #################### subroutine header begin ####################
143  
144 =head2 calltree
145  
146 Usage : calltree()
147 Purpose : Initial preperations for calltree generation for
148 user given functions.
149 Returns : None
150 Argument : Takes a reference to list of functions
151 Throws : None
152 Comment : None
153 See Also :
154  
155 =cut
156  
157 #################### subroutine header end ####################
158  
159 0 ▶sub calltree() {
Subroutine calltree: not called CC=3 SCAR=24.8
Pod documented
160 0 my ( $class, $functions ) = @_;
161 0 my @funclist = ();
162 0 ▶ if ( defined($functions) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $functions) { } 0 0 0
163 0 @funclist = @{$functions};
164 }
165 else {
166 0 @funclist = qw(main);
167 }
168 0 foreach my $function (@funclist) {
169 0 &prepareCalltreeInit( \$function );
170 }
171 0 return;
172 }
173  
174 #################### subroutine header begin ####################
175  
176 =head2 getListOfCFiles
177  
178 Usage : getListOfCFiles()
179 Purpose : Takes folder name and GNU C Preprocessor options
180 and returns list of C files in dir/subdir
181 Returns : reference to array of C files
182 Argument : folder name and GNU C Preprocessor options
183 Throws : None
184 Comment : None
185 See Also :
186 None
187  
188 =cut
189  
190 #################### subroutine header end ####################
191  
192 0 ▶sub getListOfCFiles() {
Subroutine getListOfCFiles: not called CC=13 SCAR=52.0
Pod documented
193 0 my ( $folder, $opt ) = @_;
194 0 my @cfiles = ();
195 0 my $OS = $^O;
196 0 $$folder =~ s/\//\\/g;
197 0 ▶ if ( ( defined $OS )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OS and $OS eq "MSWin32" and $$opt eq "dir_and_subdir") 0 0 0
Condition0%0/6
defined $OS and $OS eq "MSWin32" and $$opt eq "dir_and_subdir"
!ll&&!rl&&r
000
defined $OS and $OS eq "MSWin32"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $OS and $OS eq "MSWin32" and $$opt eq "dir_and_subdir"
defined $OS $OS eq "MSWin32" $$opt eq "dir_and_subdir"
Truth table0%0/4
A and B and C
A= defined $OSB= $OS eq "MSWin32"C= $$opt eq "dir_and_subdir"
ABCresult
0XX0
10X0
1100
1111
198 && ( $OS eq "MSWin32" )
199 && ( $$opt eq "dir_and_subdir" ) )
200 {
201 0 @cfiles = `dir /b /s \"$$folder\*.c\"`;
202 }
203 0 ▶ if ( ( defined $OS ) && ( $OS eq "MSWin32" ) && ( $$opt eq "dir" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OS and $OS eq "MSWin32" and $$opt eq "dir") 0 0 0
Condition0%0/6
defined $OS and $OS eq "MSWin32" and $$opt eq "dir"
!ll&&!rl&&r
000
defined $OS and $OS eq "MSWin32"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $OS and $OS eq "MSWin32" and $$opt eq "dir"
defined $OS $OS eq "MSWin32" $$opt eq "dir"
Truth table0%0/4
A and B and C
A= defined $OSB= $OS eq "MSWin32"C= $$opt eq "dir"
ABCresult
0XX0
10X0
1100
1111
204 0 chdir $$folder;
205 0 @cfiles = `dir /b *.c`;
206 }
207 0 ▶ if ( ( defined $OS )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OS and $OS eq "linux" and $$opt eq "dir_and_subdir") 0 0 0
Condition0%0/6
defined $OS and $OS eq "linux" and $$opt eq "dir_and_subdir"
!ll&&!rl&&r
000
defined $OS and $OS eq "linux"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $OS and $OS eq "linux" and $$opt eq "dir_and_subdir"
defined $OS $OS eq "linux" $$opt eq "dir_and_subdir"
Truth table0%0/4
A and B and C
A= defined $OSB= $OS eq "linux"C= $$opt eq "dir_and_subdir"
ABCresult
0XX0
10X0
1100
1111
208 && ( $OS eq "linux" )
209 && ( $$opt eq "dir_and_subdir" ) )
210 {
211 0 my $path = $$folder;
212 0 $path =~ s/\\/\//g;
213 0 chdir $path;
214 0 @cfiles = `find \. -name \*.c`;
215  
216 }
217 0 ▶ if ( ( defined $OS ) && ( $OS eq "linux" ) && ( $$opt eq "dir" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OS and $OS eq "linux" and $$opt eq "dir") 0 0 0
Condition0%0/6
defined $OS and $OS eq "linux" and $$opt eq "dir"
!ll&&!rl&&r
000
defined $OS and $OS eq "linux"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $OS and $OS eq "linux" and $$opt eq "dir"
defined $OS $OS eq "linux" $$opt eq "dir"
Truth table0%0/4
A and B and C
A= defined $OSB= $OS eq "linux"C= $$opt eq "dir"
ABCresult
0XX0
10X0
1100
1111
218 0 chdir $$folder;
219 0 @cfiles = `find *.c`;
220 }
221 0 return ( \@cfiles );
222 }
223  
224 #################### subroutine header begin ####################
225  
226 =head2 prepareCalltreeInit
227  
228 Usage : prepareCalltreeInit()
229 Purpose : final preparations for calltree generation
230 Returns : none
231 Argument : function name
232 Throws : None
233 Comment : None
234 See Also :
235 None
236  
237 =cut
238  
239 #################### subroutine header end ####################
240  
241 0 ▶sub prepareCalltreeInit() {
Subroutine prepareCalltreeInit: not called CC=5 SCAR=34.0
Pod documented
242 0 my ($function) = shift;
243 0 my $localtime = localtime();
244 0 my @calls = ();
245 0 ▶ if ( exists $calls{$$function} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $calls{$$function}) { } 0 0 0
246 0 @calls = @{ $calls{$$function} };
247 }
248 else {
249 0 print "Function does not exists\n";
250 0 return;
251 }
252 0 ▶ if ( defined($$function) && defined( $calls[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$function and defined $calls[0]) 0 0 0
Condition0%0/3
defined $$function and defined $calls[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$function and defined $calls[0]
defined $$function defined $calls[0]
Truth table0%0/3
A and B
A= defined $$functionB= defined $calls[0]
ABresult
0X0
100
111
253 0 print "<0>$$function$calls[0]\n";
254 }
255 0 $| = 1;
256 0 shift(@calls);
257 0 my $calltablelen = scalar(@calls);
258 0 ▶ if ( $calltablelen == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calltablelen == 0) 0 0 0
259 0 print "Sorry! Function \"$$function\" does not contain any calls";
260 0 return;
261 }
262 0 &generateCalltree( $$function, 1 );
263 0 print "\n";
264 0 return;
265 }
266  
267 #################### subroutine header begin ####################
268  
269 =head2 generateCalltree
270  
271 Usage : generateCalltree()
272 Purpose : Functions that actually generates the functional calltree
273 Returns : none
274 Argument : function name and tab count
275 Throws : None
276 Comment : None
277 See Also :
278 None
279  
280 =cut
281  
282 #################### subroutine header end ####################
283  
284 0 ▶sub generateCalltree() {
Subroutine generateCalltree: not called CC=14 SCAR=53.5
Pod documented
285 0 my ( $function, $tabcount ) = ( shift, shift );
286 0 ▶ if ( $calls{$function} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calls{$function}) 0 0 0
287 0 push( @rec_track, $function );
288 0 my @calls = @{ $calls{$function} };
289 0 shift(@calls);
290 0 foreach my $call (@calls) {
291 0 my $curr_cnt = $tabcount;
292 0 my $temp_call = $call;
293  
294 0 ▶ if ( $temp_call =~ /^\[/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_call =~ /^\[/) 0 0 0
295 0 next;
296 }
297 0 $temp_call =~ /(\w+)\s*\(/;
298 0 $temp_call = $1;
299 0 $temp_call = trim($temp_call);
300 0 my $temp_fun = $function;
301 0 $temp_fun = trim($temp_fun);
302 0 my $is_there = 0;
303 0 foreach my $element (@rec_track) {
304  
305 0 ▶ if ( $element eq $temp_call ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element eq $temp_call) 0 0 0
306 0 $is_there = 1;
307  
308 # $temp_element = $element;
309 0 last;
310 }
311 }
312 0 ▶ if ( $is_there eq 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_there eq 0) { } 0 0 0
313  
314 0 my $str = "";
315 0 ▶ for ( my $i = 0 ; $i < $tabcount ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $tabcount) 0 0 0
316  
317 0 print " ";
318 0 $str = "$str" . " ";
319 0 $| = 1;
320 }
321  
322 0 ▶ if ( $call =~ /^\(/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ /^\(/) { } 0 0 0
323 0 $call = "__double_def" . "$call";
324 0 print "<$tabcount>$call\n";
325 0 $call = "";
326 }
327 else {
328 0 print "<$tabcount>$call\n";
329 }
330  
331 0 $| = 1;
332 0 $temp_call = $call;
333 0 $temp_call =~ /(\w+)\s*\(/;
334 0 $temp_call = $1;
335 0 $temp_call = trim($temp_call);
336 0 ▶ my $tmp = "$str" . "</$temp_call>"
Branch: 1 branch
BranchTrueFalseTotal
if defined $temp_call and defined $str 0 0 0
Condition0%0/3
defined $temp_call and defined $str
!ll&&!rl&&r
000
MC/DC0%0/2
defined $temp_call and defined $str
defined $temp_call defined $str
Truth table0%0/3
A and B
A= defined $temp_callB= defined $str
ABresult
0X0
100
111
337 if ( defined($temp_call) && defined($str) );
338 0 $tmp = "";
339  
340 0 ▶ if ( defined($temp_call) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $temp_call) 0 0 0
341 0 &generateCalltree( $temp_call, $tabcount + 1 );
342 }
343  
344 }
345 else {
346 0 my $str = "";
347 0 ▶ for ( my $i = 0 ; $i < $tabcount ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $tabcount) 0 0 0
348  
349 0 print " ";
350 0 $str = "$str" . " ";
351  
352 0 $| = 1;
353 }
354  
355 0 ▶ if ( $call =~ /^\(/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call =~ /^\(/) { } 0 0 0
356 0 $call = "__double_def" . "$call";
357 0 print "<$tabcount>$call ---@\n";
358 0 $call = "";
359 }
360 else {
361 0 print "<$tabcount>$call\n";
362 }
363  
364 0 my $temp_call = $call;
365 0 $temp_call =~ /(\w+)\s*\(/;
366 0 $temp_call = $1;
367 0 $temp_call = trim($temp_call);
368 0 $| = 1;
369  
370 }
371 }
372 }
373 0 return;
374 }
375  
376 #################### subroutine header begin ####################
377  
378 =head2 runGnuPreprocessor
379  
380 Usage : runGnuPreprocessor()
381 Purpose : runs GNU C preprocessor in the user given path
382 Returns : returns a reference to list of names of preprocessed files
383 Argument : reference of list of C files, CPP Path, Options
384 Throws : None
385 Comment : None
386 See Also :
387 None
388  
389 =cut
390  
391 #################### subroutine header end ####################
392  
393 0 ▶sub runGnuPreprocessor() {
Subroutine runGnuPreprocessor: not called CC=3 SCAR=24.8
Pod documented
394 0 my ( $cfiles, $cppPath, $cppOpts ) = @_;
395 0 my ( $prepfile, $prep_str );
396 0 my @ppfiles = ();
397 0 my $len = scalar(@$$cfiles);
398 0 my $cnt = 0;
399 0 foreach my $cfile (@$$cfiles) {
400 0 $cnt++;
401 0 chomp($cfile);
402 0 $cfile =~ s/\\/\//g;
403 0 &progress_bar( $cnt, $len, 50, '=' );
404 0 ▶ if ( $cfile =~ /(.*)(\.c|\.C)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfile =~ /(.*)(\.c|\.C)$/) 0 0 0
405 0 my $stripfile = trim($1);
406 0 $stripfile =~ s/\\/\//g;
407 0 $prepfile = "$stripfile" . "._pp";
408 }
409 $prep_str =
410 0 "\"$$cppPath" . "/cpp\"" . " "
411 . "$$cppOpts" . " "
412 . "\"$cfile\"" . " "
413 . "> \"$prepfile\" 2>junk.txt";
414 0 push( @ppfiles, $prepfile );
415 0 system($prep_str);
416 }
417 0 print "\n";
418 0 @$$cfiles = ();
419 0 return ( \@ppfiles );
420 }
421  
422 #################### subroutine header begin ####################
423  
424 =head2 identifyFunctionsAndCalls
425  
426 Usage : identifyFunctionsAndCalls()
427 Purpose : initial preperation for parsing each C file to identify
428 functions and calls
429 Returns : none
430 Argument : reference to list of preprocessor files, options and folder
431 names.
432 Throws : None
433 Comment : None
434 See Also :
435 None
436  
437 =cut
438  
439 #################### subroutine header end ####################
440  
441 0 ▶sub identifyFunctionsAndCalls() {
Subroutine identifyFunctionsAndCalls: not called CC=2 SCAR=17.9
Pod documented
442 0 my ( $ppfiles, $opt, $folder ) = @_;
443 0 my $fun_calls;
444 0 foreach my $ppfile (@$$ppfiles) {
445 0 $fun_calls = parseCFile( \$ppfile, \$$opt, \$$folder );
446 0 updateHashTable( \$fun_calls );
447 }
448 0 return;
449 }
450  
451 #################### subroutine header begin ####################
452  
453 =head2 parseCFile
454  
455 Usage : parseCFile()
456 Purpose : Parser module to identify functions and calls in C files.
457 Returns : reference to array of funs and calls.
458 Argument : reference to filename, options and foldername
459 Throws : None
460 Comment : None
461 See Also :
462 None
463  
464 =cut
465  
466 #################### subroutine header end ####################
467  
468 0 ▶sub parseCFile() {
Subroutine parseCFile: not called CC=25 SCAR=64.8
Pod documented
469 0 my ( $infile, $opt_s, $FolderName ) = shift;
470 0 my @t_funs_calls = ();
471 0 my @pplines = ();
472 0 my $OpenCount = 0;
473 0 my $CloseCount = 0;
474 0 my $lno = 0;
475 0 my $fragment;
476 my $filename;
477 0 my $fun;
478 0 ▶ open( PPFILE, "<$$infile" ) || die("Cannot open input file $$infile\n");
Branch: 1 branch
BranchTrueFalseTotal
open PPFILE, "<$$infile" or die "Cannot open input file $$infile\n" 0 0 0
479 0 @pplines = <PPFILE>;
480 0 close(PPFILE);
481  
482 0 foreach my $ppfile (@pplines) {
483 0 $lno++;
484 0 $ppfile =~ s/\".*\(.*\"/ /g;
485  
486 0 ▶ if ( $ppfile =~ /^\#\s*([0-9]+)\s*\"(.*)\s*\"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppfile =~ /^\#\s*([0-9]+)\s*\"(.*)\s*\"/) 0 0 0
487 0 $lno = $1;
488 0 $lno--;
489 0 $filename = trim($2);
490 0 next;
491 }
492 0 ▶ while ( $ppfile =~ /(\w+)\s*\(/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppfile =~ /(\w+)\s*\(/g) 0 0 0
493 0 my $t_fun = $1;
494 0 $t_fun = trim($t_fun);
495 0 ▶ if ( ( $t_fun eq "if" )
Branch: 1 branch
BranchTrueFalseTotal
if ($t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long" or $t_fun eq "short" or $t_fun eq "bit" or $t_fun eq "unsigned" or $t_fun eq "return") 0 0 0
Condition0%0/39
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long" or $t_fun eq "short" or $t_fun eq "bit" or $t_fun eq "unsigned" or $t_fun eq "return"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long" or $t_fun eq "short" or $t_fun eq "bit" or $t_fun eq "unsigned"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long" or $t_fun eq "short" or $t_fun eq "bit"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long" or $t_fun eq "short"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while"
l!l&&r!l&&!r
000
$t_fun eq "if" or $t_fun eq "for"
l!l&&r!l&&!r
000
MC/DC0%0/14
$t_fun eq "if" or $t_fun eq "for" or $t_fun eq "while" or $t_fun eq "switch" or $t_fun eq "case" or $t_fun eq "int" or $t_fun eq "char" or $t_fun eq "flaot" or $t_fun eq "double" or $t_fun eq "long" or $t_fun eq "short" or $t_fun eq "bit" or $t_fun eq "unsigned" or $t_fun eq "return"
$t_fun eq "if" $t_fun eq "for" $t_fun eq "while" $t_fun eq "switch" $t_fun eq "case" $t_fun eq "int" $t_fun eq "char" $t_fun eq "flaot" $t_fun eq "double" $t_fun eq "long" $t_fun eq "short" $t_fun eq "bit" $t_fun eq "unsigned" $t_fun eq "return"
Truth table0%0/15
A or B or C or D or E or F or G or H or I or J or K or L or M or N
A= $t_fun eq "if"B= $t_fun eq "for"C= $t_fun eq "while"D= $t_fun eq "switch"E= $t_fun eq "case"F= $t_fun eq "int"G= $t_fun eq "char"H= $t_fun eq "flaot"I= $t_fun eq "double"J= $t_fun eq "long"K= $t_fun eq "short"L= $t_fun eq "bit"M= $t_fun eq "unsigned"N= $t_fun eq "return"
ABCDEFGHIJKLMNresult
1XXXXXXXXXXXXX1
01XXXXXXXXXXXX1
001XXXXXXXXXXX1
0001XXXXXXXXXX1
00001XXXXXXXXX1
000001XXXXXXXX1
0000001XXXXXXX1
00000001XXXXXX1
000000001XXXXX1
0000000001XXXX1
00000000001XXX1
000000000001XX1
0000000000001X1
000000000000011
000000000000000
496 || ( $t_fun eq "for" )
497 || ( $t_fun eq "while" )
498 || ( $t_fun eq "switch" )
499 || ( $t_fun eq "case" )
500 || ( $t_fun eq "int" )
501 || ( $t_fun eq "char" )
502 || ( $t_fun eq "flaot" )
503 || ( $t_fun eq "double" )
504 || ( $t_fun eq "long" )
505 || ( $t_fun eq "short" )
506 || ( $t_fun eq "bit" )
507 || ( $t_fun eq "unsigned" )
508 || ( $t_fun eq "return" ) )
509 {
510 0 next;
511 }
512 0 ▶ if ($opt_s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_s) { } 0 0 0
513 0 my $filelength = length($FolderName);
514 0 $fragment = substr $filename, $filelength;
515 0 $fragment = "." . "$fragment";
516 0 ▶ if ( $t_fun eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t_fun eq '') { } 0 0 0
517 ;
518 }
519 else {
520 0 $fun = "$t_fun" . "($lno, $fragment)";
521 }
522 }
523 else {
524 0 ▶ if ( $t_fun eq "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t_fun eq '') { } 0 0 0
525 0 print;
526 }
527 else {
528 0 $fun = "$t_fun" . "($lno, $filename)";
529 }
530 }
531 0 push( @t_funs_calls, $fun );
532 0 $fragment = "";
533 }
534 0 ▶ while ( $ppfile =~ /(;)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppfile =~ /(;)/g) 0 0 0
535 0 push( @t_funs_calls, $1 );
536 }
537 0 ▶ while ( $ppfile =~ /({)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppfile =~ /({)/g) 0 0 0
538 0 $OpenCount++;
539 0 push( @t_funs_calls, $1 );
540 }
541 0 ▶ while ( $ppfile =~ /(})/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ppfile =~ /(})/g) 0 0 0
542 0 $CloseCount++;
543 0 push( @t_funs_calls, $1 );
544 }
545 }
546 0 return ( \@t_funs_calls );
547 }
548  
549 #################### subroutine header begin ####################
550  
551 =head2 updateHashTable
552  
553 Usage : updateHashTable()
554 Purpose : updates function wise calls hash table
555 Returns : reference to hash table containing functions and calls
556 Argument : None
557 Throws : None
558 Comment : None
559 See Also :
560 None
561  
562 =cut
563  
564 #################### subroutine header end ####################
565  
566 0 ▶sub updateHashTable() {
Subroutine updateHashTable: not called CC=15 SCAR=54.8
Pod documented
567 0 my $fun_calls = shift;
568 0 my $OpenCount = 0;
569 0 my $CloseCount = 0;
570 0 my $function;
571 0 my $FUNCTIONFOUND = 0;
572 0 my $item = -1;
573 0 my $titem;
574 0 my $cnt1 = 0;
575 0 my $call;
576 my $fun_remain;
577  
578 0 foreach my $x (@$$fun_calls) {
579 0 $item++;
580  
581 0 ▶ if ( $x eq "{" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq "{") 0 0 0
582 0 $OpenCount++;
583 }
584 0 ▶ if ( $x eq "}" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq "}") 0 0 0
585 0 $CloseCount++;
586 }
587 0 ▶ if ( !defined( @$$fun_calls[$item] )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined @$$fun_calls[$item] and defined @$$fun_calls[$item + 1]) 0 0 0
Condition0%0/3
defined @$$fun_calls[$item] and defined @$$fun_calls[$item + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined @$$fun_calls[$item] and defined @$$fun_calls[$item + 1]
defined @$$fun_calls[$item] defined @$$fun_calls[$item + 1]
Truth table0%0/3
A and B
A= defined @$$fun_calls[$item]B= defined @$$fun_calls[$item + 1]
ABresult
0X0
100
111
588 || !defined( @$$fun_calls[ $item + 1 ] ) )
589 {
590 0 next;
591 }
592 0 $titem = $item + 1;
593 0 ▶ if ( ( @$$fun_calls[$item] =~ /(\w+.*)/ )
Branch: 1 branch
BranchTrueFalseTotal
if (@$$fun_calls[$item] =~ /(\w+.*)/ and @$$fun_calls[$item + 1] eq "{" and $OpenCount == $CloseCount) 0 0 0
Condition0%0/6
@$$fun_calls[$item] =~ /(\w+.*)/ and @$$fun_calls[$item + 1] eq "{" and $OpenCount == $CloseCount
!ll&&!rl&&r
000
@$$fun_calls[$item] =~ /(\w+.*)/ and @$$fun_calls[$item + 1] eq "{"
!ll&&!rl&&r
000
MC/DC0%0/3
@$$fun_calls[$item] =~ /(\w+.*)/ and @$$fun_calls[$item + 1] eq "{" and $OpenCount == $CloseCount
@$$fun_calls[$item] =~ /(\w+.*)/ @$$fun_calls[$item + 1] eq "{" $OpenCount == $CloseCount
Truth table0%0/4
A and B and C
A= @$$fun_calls[$item] =~ /(\w+.*)/B= @$$fun_calls[$item + 1] eq "{"C= $OpenCount == $CloseCount
ABCresult
0XX0
10X0
1100
1111
594 && ( @$$fun_calls[ $item + 1 ] eq "{" )
595 && ( $OpenCount == $CloseCount ) )
596 {
597 0 $function = $1;
598 0 $function =~ /(\w+)\s*\(/;
599 0 $function = $1;
600 0 $function = trim($function);
601 0 $fun_remain = $';
602 0 $fun_remain = "(" . $fun_remain;
603 0 push( @{ $calls{$function} }, $fun_remain );
604 0 $FUNCTIONFOUND = 1;
605 }
606 0 ▶ if ( ( $FUNCTIONFOUND == 1 ) && ( $OpenCount != $CloseCount ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FUNCTIONFOUND == 1 and $OpenCount != $CloseCount) 0 0 0
Condition0%0/3
$FUNCTIONFOUND == 1 and $OpenCount != $CloseCount
!ll&&!rl&&r
000
MC/DC0%0/2
$FUNCTIONFOUND == 1 and $OpenCount != $CloseCount
$FUNCTIONFOUND == 1 $OpenCount != $CloseCount
Truth table0%0/3
A and B
A= $FUNCTIONFOUND == 1B= $OpenCount != $CloseCount
ABresult
0X0
100
111
607 0 ▶ if ( ( $x eq "{" ) || ( $x eq "}" ) || ( $x eq ";" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq '{' or $x eq '}' or $x eq ';') { } 0 0 0
Condition0%0/6
$x eq "{" or $x eq "}" or $x eq ";"
l!l&&r!l&&!r
000
$x eq "{" or $x eq "}"
l!l&&r!l&&!r
000
MC/DC0%0/3
$x eq "{" or $x eq "}" or $x eq ";"
$x eq "{" $x eq "}" $x eq ";"
Truth table0%0/4
A or B or C
A= $x eq "{"B= $x eq "}"C= $x eq ";"
ABCresult
1XX1
01X1
0011
0000
608 0 next;
609 }
610 else {
611 0 $call = $x;
612 0 push( @calls_table, $call );
613 0 $call = trim($call);
614 0 ▶ if ( defined($call) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $call) 0 0 0
615 0 push( @{ $calls{$function} }, $call );
616 }
617 }
618 }
619 }
620  
621 0 return;
622 }
623  
624 #################### subroutine header begin ####################
625  
626 =head2 trim
627  
628 Usage : trim()
629 Purpose : trims leading and trailing white spaces in strings
630 Returns : trimmed string
631 Argument : string
632 Throws : None
633 Comment : None
634 See Also :
635 None
636  
637 =cut
638  
639 #################### subroutine header end ####################
640 0 ▶sub trim($) {
Subroutine trim: not called CC=3 SCAR=24.8
Pod documented
641 0 my $string = shift;
642 0 ▶ $string =~ s/^\s+// if defined($string);
Branch: 1 branch
BranchTrueFalseTotal
if defined $string 0 0 0
643 0 ▶ $string =~ s/\s+$// if defined($string);
Branch: 1 branch
BranchTrueFalseTotal
if defined $string 0 0 0
644 0 return $string;
645 }
646  
647 #################### subroutine header begin ####################
648  
649 =head2 clean
650  
651 Usage : clean()
652 Purpose : safe exit
653 Returns : none
654 Argument : none
655 Throws : None
656 Comment : None
657 See Also :
658 None
659  
660 =cut
661  
662 #################### subroutine header end ####################
663 0 ▶sub clean() {
Subroutine clean: not called CC=1 SCAR=6.9
Pod documented
664 0 %calls = ();
665 0 @calls_table = ();
666 0 @rec_track = ();
667 0 return;
668 }
669  
670 #################### subroutine header begin ####################
671  
672 =head2 progress_bar
673  
674 Usage : progress_bar()
675 Purpose : simple and neat progress bar
676 Returns : none
677 Argument : none
678 Throws : None
679 Comment : None
680 See Also :
681 None
682  
683 =cut
684  
685 #################### subroutine header end ####################
686  
687 0 ▶sub progress_bar {
Subroutine progress_bar: not called CC=3 SCAR=24.8
Pod documented
688 0 my ( $got, $total, $width, $char ) = @_;
689 0 ▶ $width ||= 25;
Condition0%0/2
$width ||= 25
l!l
00
MC/DC0%0/1
$width ||= 25
$width
Truth table0%0/2
A
A= $width
Aresult
11
00
690 0 ▶ $char ||= '=';
Condition0%0/2
$char ||= "="
l!l
00
MC/DC0%0/1
$char ||= "="
$char
Truth table0%0/2
A
A= $char
Aresult
11
00
691 0 my $num_width = length $total;
692 0 local $| = 1;
693 0 printf "[%-${width}s] processed [%${num_width}s/%s] (%.2f%%)\r",
694 $char x ( ( $width - 1 ) * $got / $total ) . '>', $got, $total,
695 100 * $got / +$total;
696 }
697 #################### main pod documentation begin ###################
698 ## Below is the stub of documentation for your module.
699 ## You better edit it!
700  
701 =head1 NAME
702  
703 C::Analyzer - Generates C Call Control Flow tree for C source code
704  
705 =head1 SYNOPSIS
706  
707 use warnings;
708 use strict;
709 use C::Analyzer;
710  
711 my @functions = qw(afs_CheckServers afs_cv2string);
712 my $analyzer = new Analyzer(
713 _inputPath => "/home/foo",
714 _cppPath => "/usr/local/bin",
715 );
716 $analyzer->init();
717 # "main" function taken if no parameter passed to this method.
718 $analyzer->calltree( \@functions );
719  
720 $analyzer->clean();
721  
722 I<The synopsis above only lists the major methods and parameters. Keep checking for new additions>
723  
724 =head1 DESCRIPTION
725  
726 Creates Call stack/tree of C source code
727  
728 =head2 GETTING HELP
729  
730 If you have questions about Analyzer you can get help from the I<analyzer-users@perl.org> mailing list. You can get help
731 on subscribing and using the list by emailing I<analyzer-users-help@perl.org>.
732  
733 =head2 NOTES
734  
735 The Analyzer is evolving and there are plans to add more features, so it's good to have the latest copy.
736  
737 =head2 Architecture of Analyzer
738  
739 |-Input folder of C files-| |----Call Stack Output---|
740  
741 .------------------------.
742 | 1. #include <stdio.h> | .-.
743 | 2. | .-------. |A|
744 | 3. void main(void) | | Perl | |N| .------------------------.
745 | 4. { | | script| |A| |A| |<0>main(3, a.c) |
746 | 5. foo(); |-------| using |--|P|--|L|-------| <1>foo(5, a.c) |
747 | 6. } | | API | |I| |Y| | <2>bar(10, a.c) |
748 | 7. | |methods| |Z| `-----------------------/
749 | 8. int foo() | | | |E|
750 | 9. { | `-------' |R|
751 | 10. bar(); | `-'
752 | 11. } |
753 `-----------------------/
754  
755 =head2 Outline Usage
756  
757 =head3 C<mandatory inputs>
758  
759 Analyzer expects couple of mandatory inputs. One, folder that contains C/H files. Second, path for GNU C Preprocessor.
760  
761 for example:
762 my $analyzer = new Analyzer(
763 _inputPath => "/home/foo",
764 _cppPath => "/usr/local/bin",
765 );
766  
767 =head3 C<optional inputs>
768  
769 Analyzer expects optional inputs as well.
770
771 It allows directory and sub directory parsing. Default is directory processing. To tell analyzer module to recursively process
772 C files in all directories and sub directories, use _inputOption
773
774 for example:
775 my $analyzer = new Analyzer(
776 _inputPath => "/home/foo",
777 _cppPath => "/usr/local/bin",
778 _inputOption => "dir_and_subdir",
779 );
780  
781 There is an option to provide additional GNU C Preprocessor options using "_cppOptions"
782
783 for example:
784 my $analyzer = new Analyzer(
785 _inputPath => "/home/foo",
786 _cppPath => "/usr/local/bin",
787 _inputOption => "dir_and_subdir",
788 _cppOptions => "-DMACRO1 -DMACRO2",
789 );
790  
791 =head1 BUGS
792  
793 None.
794  
795  
796 =head1 SUPPORT
797  
798 The Analyzer is free Open Source software. IT COMES WITHOUT WARRANTY OF ANY KIND.
799 Please let me know if you could add more features for this module.I will be more than
800 happy to add them.
801
802 =head1 AUTHOR
803  
804 Sreekanth Kocharlakota
805 CPAN ID: bmpOg
806 Sreekanth Kocharlakota
807 sreekanth@cpan.org
808 http://www.languagesemantics.com
809  
810 =head1 COPYRIGHT
811  
812 The Analyzer module is Copyright (c) 1994-2007 Sreekanth Kocharlakota. USA.
813 This program is free software licensed under the...
814  
815 The General Public License (GPL)
816 Version 2, June 1991
817  
818 The full text of the license can be found in the
819 LICENSE file included with this module.
820  
821  
822 =head1 SEE ALSO
823  
824 perl(1).
825  
826 =cut
827  
828 #################### main pod documentation end ###################
829  
830 1 1;
831  
832 # The preceding line will help the module return a true value
833  
↑ Summary