mc-manual.xml fix new/delete expresions -> expression
[valgrind.git] / tests / check_headers_and_includes
blob70e134896d0ad99e439d9eb5cc61bb1c865e7bdb
1 #!/usr/bin/env perl
3 #-------------------------------------------------------------------
4 # Check header files and #include directives
6 # (1) include/*.h must not include pub_core_...h
7 # (2) coregrind/pub_core_xyzzy.h may include pub_tool_xyzzy.h
8 # other coregrind headers may not include pub_tool_xyzzy.h
9 # (3) coregrind/ *.c must not include pub_tool_xyzzy.h
10 # (4) tool *.[ch] files must not include pub_core_...h
11 # (5) include pub_core/tool_clreq.h instead of valgrind.h except in tools'
12 # export headers
13 # (6) coregrind/ *.[ch] must not use tl_assert
14 # (7) include/*.h and tool *.[ch] must not use vg_assert
15 # (8) coregrind/ *.[ch] must not use VG_(tool_panic)
16 # (9) include/*.h and tool *.[ch] must not use VG_(core_panic)
17 # (10) *.S must unconditionally instantiate MARK_STACK_NO_EXEC
19 # There can be false positives as we don't really parse the source files.
20 # Instead we only match regular expressions.
21 #-------------------------------------------------------------------
23 use strict;
24 use warnings;
25 use File::Basename;
26 use Getopt::Long;
28 my $this_script = basename($0);
30 # The list of top-level directories is divided into three sets:
32 # (1) coregrind directories
33 # (2) tool directories
34 # (3) directories to ignore
36 # If a directory is found that does not belong to any of those sets, the
37 # script will terminate unsuccessfully.
39 my %coregrind_dirs = (
40 "include" => 1,
41 "coregrind" => 1,
44 my %tool_dirs = (
45 "memcheck" => 1,
46 "cachegrind" => 1,
47 "callgrind" => 1,
48 "helgrind", => 1,
49 "drd" => 1,
50 "massif" => 1,
51 "dhat" => 1,
52 "lackey" => 1,
53 "none" => 1,
54 "exp-bbv" => 1,
55 "shared" => 1,
58 my %dirs_to_ignore = (
59 ".deps" => 1,
60 ".git" => 1,
61 ".in_place" => 1,
62 "Inst" => 1, # the nightly scripts creates this
63 "VEX" => 1,
64 "docs" => 1,
65 "auxprogs" => 1,
66 "autom4te.cache" => 1,
67 "nightly" => 1,
68 "perf" => 1,
69 "tests" => 1,
70 "gdbserver_tests" => 1,
71 "mpi" => 1,
72 "solaris" => 1
75 my %tool_export_header = (
76 "dhat/dhat.h" => 1,
77 "drd/drd.h" => 1,
78 "helgrind/helgrind.h" => 1,
79 "memcheck/memcheck.h" => 1,
80 "callgrind/callgrind.h" => 1,
81 "cachegrind/cachegrind.h" => 1
84 my $usage=<<EOF;
85 USAGE
87 $this_script
89 [--debug] Debugging output
91 dir ... Directories to process
92 EOF
94 my $debug = 0;
95 my $num_errors = 0;
97 &main;
99 sub main {
100 GetOptions( "debug" => \$debug ) || die $usage;
102 my $argc = $#ARGV + 1;
104 if ($argc < 1) {
105 die $usage;
108 foreach my $dir (@ARGV) {
109 process_dir(undef, $dir, 0);
112 my $rc = ($num_errors == 0) ? 0 : 1;
113 exit $rc;
116 sub process_dir {
117 my ($path, $dir, $depth) = @_;
118 my $hdir;
120 if ($depth == 0) {
121 # The root directory is always processed
122 } elsif ($depth == 1) {
123 # Toplevel directories
124 return if ($dirs_to_ignore{$dir});
126 if (! $tool_dirs{$dir} && ! $coregrind_dirs{$dir}) {
127 die "Unknown directory '$dir'. Please update $this_script\n";
129 } else {
130 # Subdirectories
131 return if ($dirs_to_ignore{$dir});
134 print "DIR = $dir DEPTH = $depth\n" if ($debug);
136 chdir($dir) || die "Cannot chdir '$dir'\n";
138 opendir($hdir, ".") || die "cannot open directory '.'";
140 while (my $file = readdir($hdir)) {
141 next if ($file eq ".");
142 next if ($file eq "..");
144 # Subdirectories
145 if (-d $file) {
146 my $full_path = defined $path ? "$path/$file" : $file;
147 process_dir($full_path, $file, $depth + 1);
148 next;
151 # Regular files; only interested in *.c, *.S and *.h
152 next if (! ($file =~ /\.[cSh]$/));
153 my $path_name = defined $path ? "$path/$file" : $file;
154 process_file($path_name);
156 close($hdir);
157 chdir("..") || die "Cannot chdir '..'\n";
160 #---------------------------------------------------------------------
161 # Return 1, if file is located in <valgrind>/include
162 #---------------------------------------------------------------------
163 sub is_coregrind_export_header {
164 my ($path_name) = @_;
166 return ($path_name =~ /^include\//) ? 1 : 0;
169 #---------------------------------------------------------------------
170 # Return 1, if file is located underneath <valgrind>/coregrind
171 #---------------------------------------------------------------------
172 sub is_coregrind_file {
173 my ($path_name) = @_;
175 return ($path_name =~ /^coregrind\//) ? 1 : 0;
178 #---------------------------------------------------------------------
179 # Return 1, if file is located underneath <valgrind>/<tool>
180 #---------------------------------------------------------------------
181 sub is_tool_file {
182 my ($path_name) = @_;
184 for my $tool (keys %tool_dirs) {
185 return 1 if ($path_name =~ /^$tool\//);
187 return 0
190 #---------------------------------------------------------------------
191 # Return array of files #include'd by file.
192 #---------------------------------------------------------------------
193 sub get_included_files {
194 my ($path_name) = @_;
195 my @includes = ();
196 my $file = basename($path_name);
198 open(FILE, "<$file") || die "Cannot open file '$file'";
200 while (my $line = <FILE>) {
201 if ($line =~ /^\s*#\s*include "([^"]*)"/) {
202 push @includes, $1;
204 if ($line =~ /^\s*#\s*include <([^>]*)>/) {
205 push @includes, $1;
208 close FILE;
209 return @includes;
212 #---------------------------------------------------------------------
213 # Check a file from <valgrind>/include
214 #---------------------------------------------------------------------
215 sub check_coregrind_export_header {
216 my ($path_name) = @_;
217 my $file = basename($path_name);
219 foreach my $inc (get_included_files($path_name)) {
220 $inc = basename($inc);
221 # Must not include pub_core_....
222 if ($inc =~ /pub_core_/) {
223 error("File $path_name must not include $inc\n");
225 # Only pub_tool_clreq.h may include valgrind.h
226 if (($inc eq "valgrind.h") && ($path_name ne "include/pub_tool_clreq.h")) {
227 error("File $path_name should include pub_tool_clreq.h instead of $inc\n");
230 # Must not use vg_assert
231 my $assert = `grep vg_assert $file`;
232 if ($assert ne "") {
233 error("File $path_name must not use vg_assert\n");
235 # Must not use VG_(core_panic)
236 my $panic = `grep 'VG_(core_panic)' $file`;
237 if ($panic ne "") {
238 error("File $path_name must not use VG_(core_panic)\n");
242 #---------------------------------------------------------------------
243 # Check a file from <valgrind>/coregrind
244 #---------------------------------------------------------------------
245 sub check_coregrind_file {
246 my ($path_name) = @_;
247 my $file = basename($path_name);
249 foreach my $inc (get_included_files($path_name)) {
250 print "\tINCLUDE $inc\n" if ($debug);
251 # Only pub_tool_xyzzy.h may include pub_core_xyzzy.h
252 if ($inc =~ /pub_tool_/) {
253 my $buddy = $inc;
254 $buddy =~ s/pub_tool/pub_core/;
255 if ($file ne $buddy) {
256 error("File $path_name must not include $inc\n");
259 # Must not include valgrind.h
260 if ($inc eq "valgrind.h") {
261 error("File $path_name should include pub_core_clreq.h instead of $inc\n");
264 # Must not use tl_assert
265 my $assert = `grep tl_assert $file`;
266 if ($assert ne "") {
267 error("File $path_name must not use tl_assert\n");
269 # Must not use VG_(tool_panic)
270 my $panic = `grep 'VG_(tool_panic)' $file`;
271 if ($panic ne "") {
272 chomp($panic);
273 # Do not complain about the definition of VG_(tool_panic)
274 if (($path_name eq "coregrind/m_libcassert.c") &&
275 ($panic eq "void VG_(tool_panic) ( const HChar* str )")) {
276 # OK
277 } else {
278 error("File $path_name must not use VG_(tool_panic)\n");
283 #---------------------------------------------------------------------
284 # Check a file from <valgrind>/<tool>
285 #---------------------------------------------------------------------
286 sub check_tool_file {
287 my ($path_name) = @_;
288 my $file = basename($path_name);
290 foreach my $inc (get_included_files($path_name)) {
291 print "\tINCLUDE $inc\n" if ($debug);
292 # Must not include pub_core_...
293 if ($inc =~ /pub_core_/) {
294 error("File $path_name must not include $inc\n");
296 # Must not include valgrind.h unless this is an export header
297 if ($inc eq "valgrind.h" && ! $tool_export_header{$path_name}) {
298 error("File $path_name should include pub_tool_clreq.h instead of $inc\n");
301 # Must not use vg_assert
302 my $assert = `grep vg_assert $file`;
303 if ($assert ne "") {
304 error("File $path_name must not use vg_assert\n");
306 # Must not use VG_(core_panic)
307 my $panic = `grep 'VG_(core_panic)' $file`;
308 if ($panic ne "") {
309 error("File $path_name must not use VG_(core_panic)\n");
313 #---------------------------------------------------------------------
314 # Check an assembler file
315 #---------------------------------------------------------------------
316 sub check_assembler_file {
317 my ($path_name) = @_;
318 my $file = basename($path_name);
319 my $found = 0;
321 open(FILE, "<$file") || die "Cannot open file '$file'";
323 while (my $line = <FILE>) {
324 if ($line =~ /^\s*MARK_STACK_NO_EXEC/) {
325 $found = 1;
326 last;
329 if ($found == 0) {
330 error("File $path_name does not instantiate MARK_STACK_NO_EXEC\n");
331 } else {
332 while (my $line = <FILE>) {
333 if ($line =~ /^\s*#\s*endif/) {
334 error("File $path_name instantiates MARK_STACK_NO_EXEC"
335 . " under a condition\n");
336 last;
340 close FILE;
343 sub process_file {
344 my ($path_name) = @_;
346 print "FILE = $path_name\n" if ($debug);
348 if (is_coregrind_export_header($path_name)) {
349 check_coregrind_export_header($path_name);
350 } elsif (is_coregrind_file($path_name)) {
351 check_coregrind_file($path_name);
352 } elsif (is_tool_file($path_name)) {
353 check_tool_file($path_name);
356 if ($path_name =~ /\.S$/) {
357 check_assembler_file($path_name);
361 sub error {
362 my ($message) = @_;
363 print STDERR "*** $message";
364 ++$num_errors;