]>
Commit | Line | Data |
---|---|---|
3d41e53a FC |
1 | #!/usr/bin/perl |
2 | # | |
f70aedc4 | 3 | ## Copyright (C) 1996-2021 The Squid Software Foundation and contributors |
a151895d AJ |
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 | ||
3d41e53a FC |
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. | |
47f28373 | 14 | # |
3d41e53a FC |
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") | |
2f8abb64 | 17 | # is sorted with this specification: squid.h (if present) is always first, |
3d41e53a FC |
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; | |
0c2d21bb AR |
25 | |
26 | my %Seen = (); # preprocessor #include lines, indexed by file name | |
27 | ||
3d41e53a | 28 | while (<>) { |
47f28373 FC |
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; | |
3d41e53a | 43 | } |
3d41e53a | 44 | } |
0c2d21bb | 45 | &dumpSeen(); |
3d41e53a | 46 | |
0c2d21bb | 47 | sub dumpSeen { |
47f28373 FC |
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 = (); | |
3d41e53a | 55 | } |