Debian Codegraph @

scripts/clang-format.pl

raw
1 #!/usr/bin/perl
2 # -*- mode: cperl; indent-tabs-mode: nil; tab-width: 3; cperl-indent-level: 3; -*-
3 use warnings;
4 use strict;
5 use utf8;
6
7 sub file_read {
8 my ($name) = @_;
9 local $/ = undef;
10 open FILE, '<'.$name or die "Could not open $name: $!\n";
11 my $data = <FILE>;
12 close FILE;
13 return $data;
14 }
15
16 sub file_write {
17 my ($name,$data) = @_;
18 open FILE, '>'.$name or die "Could not open $name: $!\n";
19 print FILE $data;
20 close FILE;
21 }
22
23 chdir('src/');
24 my @files = glob('*.c *.h *.cpp *.hpp');
25
26 foreach my $file (@files) {
27 # Protect preprocessor directives due to https://llvm.org/bugs/show_bug.cgi?id=17362
28 my $data = file_read($file);
29 $data =~ s@#pragma once\n#ifndef@PRAGMA_ONCE_IFNDEF@g;
30 $data =~ s@([ \t]*#if.+?#endif\n)@// clang-format off\n$1// clang-format on\n@sg;
31 $data =~ s@\n// clang-format on\n// clang-format off\n@\n@g;
32 $data =~ s@PRAGMA_ONCE_IFNDEF@#pragma once\n#ifndef@g;
33 file_write($file, $data);
34
35 `clang-format-6.0 -style=file -i '$file'`;
36
37 my $data = file_read($file);
38 $data =~ s@\n[^\n]*//[^\n]+clang-format (off|on)\n@\n@g; # Remove preprocessor protection
39 # Things clang-format gets wrong:
40 $data =~ s@([ \t]+)(BOOST_FOREACH|boost_foreach|reverse_foreach|foreach)([^{\n]*) \{[ \t]+([^}\n]*)[ \t]+\}@$1$2$3 {\n$1\t$4\n$1}@g; # Don't allow single-line foreach blocks
41 $data =~ s@(operator .+?) \(@$1(@g; # No space before ( in operators
42 $data =~ s@ \*([,>)])@*$1@g; # No space before statement-final *
43 $data =~ s@^([ \t]*)( [:,] [^\n]+) \{@$1$2\n$1\{@mg; # { after ctor-init should go on its own line
44 $data =~ s@template <@template<@g; # No space in template<
45
46 # clang-format horribly mangles enums if AlignConsecutiveAssignments is off, so fix that
47 my @enums = ($data =~ m@enum [^{]*\{(.+?)\n\s*\}[^;\n]*;@sg);
48 foreach my $enum (@enums) {
49 my @lines = split /\n/, $enum;
50 my $len = 0;
51 foreach my $line (@lines) {
52 if ($line =~ m@^(\s*\S+) = @) {
53 if (length($1) > $len) {
54 $len = length($1);
55 }
56 }
57 }
58
59 my @comb = ();
60 foreach my $line (@lines) {
61 if ($line =~ m@^(\s*\S+) = @) {
62 my $sps = ' ' x (1 + $len - length($1));
63 $line =~ s@ = @$sps= @;
64 }
65 $line =~ s@\(1 << (\d)\)@(1 << $1)@;
66 push @comb, $line;
67 }
68 my $comb = join "\n", @comb;
69 $data =~ s@\Q$enum\E@$comb@g;
70 }
71
72 # clang-format has no idea what I want with UOptions[], so fix that as well
73 my @enums = ($data =~ m@UOption options\[\] = \{(.+?)\n\s*\}[^;\n]*;@sg);
74 foreach my $enum (@enums) {
75 my @lines = split /\n/, $enum;
76 my $len = 0;
77 foreach my $line (@lines) {
78 if ($line =~ m@^([^"]+"[^"]+",)@) {
79 if (length($1) > $len) {
80 $len = length($1);
81 }
82 }
83 }
84
85 my @comb = ();
86 foreach my $line (@lines) {
87 if ($line =~ m@^([^"]+"[^"]+",)@) {
88 my $txt = $1;
89 my $sps = ' ' x (1 + $len - length($txt));
90 my ($is) = ($txt =~ m@^(\s+)@);
91 $line =~ s@\s+@ @g;
92 $line =~ s@, 0, @, 0, @g;
93 $line =~ s@UOPT_NO_ARG, @UOPT_NO_ARG, @g;
94 $line =~ s@^\s+@$is@;
95 $line =~ s@(\Q$txt\E) @$1$sps@;
96 }
97 push @comb, $line;
98 }
99 my $comb = join "\n", @comb;
100 $data =~ s@\Q$enum\E@$comb@g;
101 }
102
103 file_write($file, $data);
104 }