21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 1) #!/usr/bin/env perl
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 2) # SPDX-License-Identifier: GPL-2.0
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 3) #
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 4) # checkdeclares: find struct declared more than once
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 5) #
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 6) # Copyright 2021 Wan Jiabing<wanjiabing@vivo.com>
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 7) # Inspired by checkincludes.pl
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 8) #
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 9) # This script checks for duplicate struct declares.
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 10) # Note that this will not take into consideration macros so
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 11) # you should run this only if you know you do have real dups
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 12) # and do not have them under #ifdef's.
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 13) # You could also just review the results.
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 14)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 15) use strict;
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 16)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 17) sub usage {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 18) print "Usage: checkdeclares.pl file1.h ...\n";
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 19) print "Warns of struct declaration duplicates\n";
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 20) exit 1;
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 21) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 22)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 23) if ($#ARGV < 0) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 24) usage();
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 25) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 26)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 27) my $dup_counter = 0;
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 28)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 29) foreach my $file (@ARGV) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 30) open(my $f, '<', $file)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 31) or die "Cannot open $file: $!.\n";
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 32)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 33) my %declaredstructs = ();
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 34)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 35) while (<$f>) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 36) if (m/^\s*struct\s*(\w*);$/o) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 37) ++$declaredstructs{$1};
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 38) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 39) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 40)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 41) close($f);
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 42)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 43) foreach my $structname (keys %declaredstructs) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 44) if ($declaredstructs{$structname} > 1) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 45) print "$file: struct $structname is declared more than once.\n";
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 46) ++$dup_counter;
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 47) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 48) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 49) }
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 50)
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 51) if ($dup_counter == 0) {
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 52) print "No duplicate struct declares found.\n";
21917bded72cf (Wan Jiabing 2021-04-29 22:53:56 -0700 53) }