Skip to content

Commit

Permalink
scripts: a new script for checking duplicate struct declaration
Browse files Browse the repository at this point in the history
checkdeclares: find struct declared more than once.  Inspired by
checkincludes.pl.

This script checks for duplicate struct declares.  Note that this will not
take into consideration macros, so you should run this only if you know
you do have real dups and do not have them under #ifdef's.  You could also
just review the results.

[[email protected]: fix usage message, grammar]

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Wan Jiabing <[email protected]>
Cc: Masahiro Yamada <[email protected]>
Cc: Joe Perches <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Wan Jiabing authored and torvalds committed Apr 30, 2021
1 parent 439baed commit 21917bd
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions scripts/checkdeclares.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env perl
# SPDX-License-Identifier: GPL-2.0
#
# checkdeclares: find struct declared more than once
#
# Copyright 2021 Wan Jiabing<[email protected]>
# Inspired by checkincludes.pl
#
# This script checks for duplicate struct declares.
# Note that this will not take into consideration macros so
# you should run this only if you know you do have real dups
# and do not have them under #ifdef's.
# You could also just review the results.

use strict;

sub usage {
print "Usage: checkdeclares.pl file1.h ...\n";
print "Warns of struct declaration duplicates\n";
exit 1;
}

if ($#ARGV < 0) {
usage();
}

my $dup_counter = 0;

foreach my $file (@ARGV) {
open(my $f, '<', $file)
or die "Cannot open $file: $!.\n";

my %declaredstructs = ();

while (<$f>) {
if (m/^\s*struct\s*(\w*);$/o) {
++$declaredstructs{$1};
}
}

close($f);

foreach my $structname (keys %declaredstructs) {
if ($declaredstructs{$structname} > 1) {
print "$file: struct $structname is declared more than once.\n";
++$dup_counter;
}
}
}

if ($dup_counter == 0) {
print "No duplicate struct declares found.\n";
}

0 comments on commit 21917bd

Please sign in to comment.