forked from glotzerlab/hoomd-blue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoomd_lint_detector.pl
173 lines (145 loc) · 4.33 KB
/
hoomd_lint_detector.pl
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use strict;
use warnings;
use File::Find;
# use Text::Diff::Parser;
use File::Temp;
# parameters for controlling what gets reported
my $max_line_len = 121;
my $overlength_threshold = 25;
my $astyle_changes_threshold = 25;
# format with astyle and count the number of lines that it changed
sub process_file_astyle
{
my ($fname, $lines) = @_;
$_ = $fname;
my $message = "";
if (/\.cc$/ or /\.h$/ or /\.cu$/ or /\.cuh$/)
{
# get a temporary file to write to
my $astyle_out_file = tmpnam();
# restyle the file
`astyle --mode=c --style=whitesmith --keep-one-line-statements --convert-tabs --fill-empty-lines -M70 < $fname > $astyle_out_file`;
# get the differences between the files
my $diffs = `diff -u $fname $astyle_out_file`;
my $parser = Text::Diff::Parser->new($diffs);
$parser->simplify();
my $total_changes = 0;
foreach my $change ( $parser->changes )
{
$total_changes += $change->size;
}
# output the number of changes
if ($total_changes > $astyle_changes_threshold)
{
$message .= "astyle changes: $total_changes\n";
}
unlink($astyle_out_file);
}
return $message;
}
# process all the lines in the file and check for tabs, eof newline, overlength conditions, etc.
sub process_file_lines
{
my $fname = $_[0];
my $fullpath = $_[1];
# for checking the final newline
my $last_line;
# open the file
open(FILE, "< $fname") or die "can't open $fname: $!";
# initialize counters to 0
my $tab_count = 0;
my $eol_whitespace_count = 0;
my $line_count = 0;
my $overlength_count = 0;
my $has_doxygen_file = 0;
my $has_doxygen_package = 0;
# loop through all lines in the file and add up counters
while (<FILE>)
{
$tab_count += tr/\t//;
$last_line = $_ if eof;
chomp();
$eol_whitespace_count += /(\s*)$/ && length($1);
if (length($_) > $max_line_len)
{
$overlength_count += 1;
}
if ($_ =~ /\\file (.*)$/)
{
$has_doxygen_file = 1;
}
if ($_ =~ /\\package (.*)$/)
{
$has_doxygen_package = 1;
}
$line_count += 1;
}
close(FILE);
my $message = "";
if ($tab_count > 0)
{
$message .= "tabs: $tab_count\n";
}
if ($eol_whitespace_count > 0)
{
$message .= "EOL whitespace: $eol_whitespace_count\n";
}
if ($overlength_count > $overlength_threshold)
{
$message .= "lines overlength: $overlength_count\n";
}
# if (!$has_doxygen_file && !($fname =~ /\.py$/ or $fullpath =~ /\/test\//))
# {
# $message .= "missing doxygen \\file\n";
# }
#if (!$has_doxygen_package && ($fullpath =~ /\/python-module\//))
# {
# $message .= "missing doxygen \\package\n";
# }
#if (not $last_line =~ /^\n/)
# {
# $message .= "end of file newline: missing\n";
# }
return ($message, $line_count);
}
sub wanted
{
my $fname = $_;
# skip processing if this file is in the extern directory
if ($File::Find::name =~ /\/extern\//)
{
return;
}
# skip processing if this file is in the build
if ($File::Find::name =~ /\/build\//)
{
return;
}
# skip processing if this file is in the microbenchmarks directory
if ($File::Find::name =~ /\/microbenchmarks\//)
{
return;
}
# skip processing if this file is in the share directory
if ($File::Find::name =~ /\/share\//)
{
return;
}
if (/\.cc$/ or /\.h$/ or /\.cu$/ or /\.cuh$/ or /\.py$/)
{
my $full_message = "";
my $message;
my $line_count;
($message, $line_count) = process_file_lines($fname, $File::Find::name);
$full_message .= $message;
#$full_message .= process_file_astyle($fname, $line_count);
if ($full_message)
{
print "$File::Find::name\n";
print $full_message;
print "\n";
}
}
}
# grep through the source and look for problems
finddepth(\&wanted, '.');