]> git.ipfire.org Git - thirdparty/squid.git/blame - scripts/sort-includes.pl
SourceFormat Enforcement
[thirdparty/squid.git] / scripts / sort-includes.pl
CommitLineData
3d41e53a
FC
1#!/usr/bin/perl
2#
4ac4a490 3## Copyright (C) 1996-2017 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.
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
23use strict;
24use warnings;
0c2d21bb
AR
25
26my %Seen = (); # preprocessor #include lines, indexed by file name
27
3d41e53a 28while (<>) {
0c2d21bb
AR
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
3d41e53a 38 }
0c2d21bb 39 $Seen{$fname} = $_;
3d41e53a 40 } else {
0c2d21bb 41 &dumpSeen();
3d41e53a
FC
42 print;
43 }
44}
0c2d21bb 45&dumpSeen();
3d41e53a 46
0c2d21bb
AR
47sub 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 = ();
3d41e53a 55}