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