Welcome to mirror list, hosted at ThFree Co, Russian Federation.

check_memleak_includes.pl « tools - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 46207533bc0b05302677d031c0178f4421e6cccb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl -w

# modules
use strict;
use File::Find;

my $ROOT = "..";
my @EXCLUDE = ("3party", "base", "std", "out", "tools", "testing");
my @SOURCE_EXT = (".cpp");
my @HEADER_EXT = (".hpp", ".h");

my $START_MD = "start_mem_debug.hpp";
my $STOP_MD = "stop_mem_debug.hpp";
my $INCLUDE_KEYWORD = "#include";

#######################
sub analyze_header {
  # search for start inlude, in header file after it should be no more includes!
  # and stop include should be the last at the end
  my $res = index($_[1], $START_MD);
  if ($res == -1) {
    print "ERROR: No memory leak detector in file $_[0]\n";
  }
  else {
    my $res2 = index($_[1], $STOP_MD, $res);
    if ($res2 == -1) {
      print "ERROR: Last line in header file $_[0] should contain #include \"$STOP_MD\"\n";
    }
    else
    {
      print "$_[0] is OK\n";
    }
  }
}

###################
sub analyze_source {
  # search for start inlude, in source file it should be only one and after it no more includes!
  my $res = index($_[1], $START_MD);
  if ($res == -1) {
    print "ERROR: No memory leak detector in file $_[0]\n";
  }
  else {
    my $res2 = index($_[1], $INCLUDE_KEYWORD, $res);
    if ($res2 != -1) {
      print "ERROR: #include \"$START_MD\" should be the last include in $_[0]\n";
    }
    else
    {
      print "$_[0] is OK\n";
    }
  }
}

###################################################################
sub process_file {
  my $fullPath = $File::Find::name;
  my $f = $_;

  # ignore tests directories
  unless ($fullPath =~ /_tests\//)
  {
    my $isSource = 0;
    foreach my $ext (@SOURCE_EXT) {
      $isSource = 1 if ($f =~ /$ext$/);
    }
    my $isHeader = 0;
    foreach my $ext (@HEADER_EXT) {
      $isHeader = 1 if ($f =~ /$ext$/);
    }

    if ($isSource or $isHeader) {
      open(INFILE, "<$f") or die("ERROR: can't open input file $fullPath\n");
      binmode(INFILE);
      my @fileAttribs = stat(INFILE);
      read(INFILE, my $buffer, $fileAttribs[7]);
      close(INFILE);
      analyze_source($fullPath, $buffer) if $isSource;
      analyze_header($fullPath, $buffer) if $isHeader;
    }
  }
}


#######################################
# ENTY POINT
#######################################

print("Scanning sources for correct memory leak detector includes\n");
my @raw_dirs = <$ROOT/*>;
my @dirs;

# filter out excluded directories
foreach my $f (@raw_dirs) {
  my $good = 1;
  foreach my $excl (@EXCLUDE) {
    if (-f $f or $f =~ /$excl/) {
      $good = 0;
    }
  }
  push(@dirs, $f) if $good;
}

# set array print delimeter
print "Directories for checking:\n@dirs\n";
find(\&process_file, @dirs);