]> git.ipfire.org Git - thirdparty/squid.git/blob - scripts/sort-includes.pl
6252dd2e38d085053107d237e309cba36480ff7f
[thirdparty/squid.git] / scripts / sort-includes.pl
1 #!/usr/bin/perl
2 #
3 ## Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 ##
5 ## Squid software is distributed under GPLv2+ license and includes
6 ## contributions from numerous individuals and organizations.
7 ## Please see the COPYING and CONTRIBUTORS files for details.
8 ##
9
10 # USAGE: sort-includes.pl filename.cc >filename.cc.sorted
11 #
12 # This tool helps to sort the #include directives in a c or c++ source file
13 # according to the Squid Coding guidelines.
14 #
15 # The output of the tool is a source file where each block of consecutive
16 # include directives for project-specific files (#include "header.h")
17 # is sorted with this specification: squid.h (if present) is alwasy first,
18 # then the other directives are sorted in case-insensitive alphabetical order.
19 #
20 # Suggested usage:
21 # for file in $(find . -name \*.cc); do /full/path/to/sort-includes.pl $file >$file.sorted; mv $file.sorted $file; done
22
23 use strict;
24 use warnings;
25
26 my %Seen = (); # preprocessor #include lines, indexed by file name
27
28 while (<>) {
29 if (/^\s*#\s*include\s*"(.+?)"/) {
30 my $fname = $1;
31 # skip repeated file names that have identical #include lines
32 if (defined $Seen{$fname}) {
33 next if $Seen{$fname} eq $_;
34 warn("$ARGV:$.: Warning: inconsistent $fname #include lines:\n");
35 warn(" $Seen{$fname}");
36 warn(" $_");
37 # fall through to preserve every unique #include line
38 }
39 $Seen{$fname} = $_;
40 } else {
41 &dumpSeen();
42 print;
43 }
44 }
45 &dumpSeen();
46
47 sub dumpSeen {
48 my $alwaysFirst = 'squid.h';
49 if (defined $Seen{$alwaysFirst}) {
50 print $Seen{$alwaysFirst};
51 delete $Seen{$alwaysFirst};
52 }
53 print sort { lc($a) cmp lc($b) } values %Seen;
54 %Seen = ();
55 }