-
Notifications
You must be signed in to change notification settings - Fork 138
/
c_header_guards.t
181 lines (139 loc) · 5.17 KB
/
c_header_guards.t
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
174
175
176
177
178
179
180
181
#! perl
# Copyright (C) 2006-2009, Parrot Foundation.
# $Id$
use strict;
use warnings;
use lib qw( . lib ../lib ../../lib );
use Test::More tests => 5;
use Parrot::Distribution;
=head1 NAME
t/codingstd/c_header_guards.t - checks for rules related to guards in C header files
=head1 SYNOPSIS
# test all files
% prove t/codingstd/c_header_guards.t
# test specific files
% perl t/codingstd/c_header_guards.t include/parrot/bar.h
=head1 DESCRIPTION
Checks that all C language header files have an
#ifndef PARROT_WHATEVER_H_GUARD definition, then they
#define PARROT_WHATEVER_H_GUARD and add an
#endif /* PARROT_WHATEVER_H_GUARD */ at the end, of the file as specified
in PDD07.
=head1 AUTHOR
Mark Glines <mark at glines dot org>
=head1 SEE ALSO
L<docs/pdds/pdd07_codingstd.pod>
=cut
my $DIST = Parrot::Distribution->new();
my @files;
if (@ARGV) {
@files = <@ARGV>;
}
else {
my %files = map { $_->path() => 1 } $DIST->c_header_files();
my $href = $DIST->generated_files();
foreach my $file ( keys %$href ) {
if ( $file =~ /\.h$/ ) {
$files{$file} = 1 if -f $file;
}
}
# not all files should be subject to the coding standards
foreach my $file ( keys %files ) {
delete $files{$file} if $DIST->is_c_exemption($file);
}
@files = sort keys %files;
}
check_header_guards(@files);
exit;
sub check_header_guards {
my ( %guardnames, %redundants, %collisions, %missing_guard, %missing_define, %missing_comment );
F: foreach my $file (@_) {
open my $fh, '<', $file
or die "Cannot open '$file' for reading!\n";
my @source = <$fh>;
close $fh;
chomp @source;
my ( $ifndef, $define, $endif );
L: foreach my $line (@source) {
$line =~ s/\s+/ /;
$line =~ s/^ //;
# skip Bison parser files
next F if $line =~ /A Bison parser/;
# skip the non-preprocessor lines
next L unless substr( $line, 0, 1 ) eq '#';
# skip the "#", and any leading whitespace
$line = substr( $line, 1 );
$line =~ s/^ //;
if ( $line =~ m{ifndef (PARROT_.+_GUARD)$} ) {
# allow include/parrot/platform.h to have redundant guards;
# it contains verbatim copies of other header files (which
# have their own guards).
next L if ( defined($ifndef) && $ifndef eq 'PARROT_PLATFORM_H_GUARD' );
# check for multiple guards in the same file
$redundants{$file} = $1 if defined $ifndef;
# check for the same guard-name in multiple files
if ( exists( $guardnames{$1} ) ) {
if ( !duplicate_files( $file, $guardnames{$1} ) ) {
$collisions{$file} = $guardnames{$1};
}
}
$ifndef = $1;
$guardnames{$1} = $file;
}
if ( $line =~ m{define (PARROT_.+_GUARD)$} ) {
$define = $1
if ( defined($ifndef) && $ifndef eq $1 );
}
if ( $line =~ m{endif /\* (PARROT_.+_GUARD) \*/$} ) {
$endif = $1
if ( defined($ifndef) && $ifndef eq $1 );
}
}
$missing_guard{$file} = 1 unless defined $ifndef;
$missing_define{$file} = 1 unless defined $define;
$missing_comment{$file} = 1 unless defined $endif;
}
ok( !%collisions, "identical PARROT_*_GUARD macro names used in headers" )
or diag( "collisions: \n" . join( ", \n", %collisions ) );
ok( !%redundants, "multiple PARROT_*_GUARD macros found in headers" )
or diag( "redundants: \n" . join( ", \n", keys %redundants ) );
ok( !%missing_guard,
"missing or misspelled PARROT_*_GUARD ifndef in headers" )
or diag( "missing guard: \n"
. join( ", \n", sort keys %missing_guard )
. "\nyou need to add a line like:\n"
. " #ifndef PARROT_*_GUARD\n"
. "at the top of the header." );
ok( !%missing_define,
"missing or misspelled PARROT_*_GUARD define in headers" )
or diag( "missing define: \n"
. join( ", \n", sort keys %missing_define )
. "\nyou need to add a line like:\n"
. " #define PARROT_*_GUARD\n"
. "at the top of the header." );
ok( !%missing_comment, "missing or misspelled PARROT_*_GUARD "
. "comment after the endif in headers" )
or diag( "missing endif comment: \n"
. join( ", \n", sort keys %missing_comment )
. "\nyou need to add a line like:\n"
. " #endif /* PARROT_*_GUARD */\n"
. "at the end of the header." );
return 0;
}
sub duplicate_files {
my ( $file1, $file2 ) = @_;
open my $fh1, '<', $file1
or die "Cannot open '$file1' for reading!\n";
open my $fh2, '<', $file2
or die "Cannot open '$file2' for reading!\n";
local $/;
$file1 = <$fh1>;
$file2 = <$fh2>;
return $file1 eq $file2;
}
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4: