1 # This file is part of the LibreOffice project.
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 our @EXPORT_OK = qw(get_excludelist set_excludelist get_wanted_version get_own_directory get_extension_regex find check_style);
14 # Reads the excludelist.
17 my $src = "c|cpp|cxx|h|hxx|inl";
18 my %excludelist_names = ();
20 # Read the excludelist.
21 if (open(LINES
, "solenv/clang-format/excludelist"))
23 while (my $line = <LINES
>)
26 $excludelist_names{$line} = 1;
30 return \
%excludelist_names;
33 # Writes the excludelist.
34 # The single argument is a reference to an array.
37 my @filenames = @
{$_[0]};
38 open my $fh, ">", "solenv/clang-format/excludelist" or die $!;
39 print $fh "$_\n" for @filenames;
43 # Returns the clang-format version used of style enforcement.
44 sub get_wanted_version
()
49 # Returns the directory that can host a binary which is used automatically, even
50 # if it's not in PATH.
51 sub get_own_directory
()
56 # Returns a regex matching filenames we clang-format.
57 sub get_extension_regex
()
59 return "c|cpp|cxx|h|hxx|inl";
62 # Use clang-format from CLANG_FORMAT, from our dedicated directory or from
63 # PATH, in this order.
66 my $version = get_wanted_version
();
67 my $opt_lo = get_own_directory
();
69 if (!(defined($ENV{CLANG_FORMAT
}) && is_matching_clang_format_version
($ENV{CLANG_FORMAT
}, $version)))
71 my @dirs = split /:/, $ENV{PATH
};
72 unshift(@dirs, $opt_lo);
74 foreach my $dir (@dirs)
76 if (is_matching_clang_format_version
("$dir/clang-format", $version))
78 $clang_format = "$dir/clang-format";
85 $clang_format = $ENV{CLANG_FORMAT
};
88 if ($^O
eq "cygwin" && defined($clang_format))
90 $clang_format = `cygpath -m '$clang_format'`;
97 # Diffs the original and the formatted version of a single file from the index.
100 # Make sure that not staged changes are not considered when diffing.
101 my ($clang_format, $filename) = @_;
102 my $index = $filename . ".index";
103 system("git show :$filename > $index");
104 my $format = $index . ".format";
105 system("'$clang_format' -assume-filename=$filename $index > $format");
106 my $ret = system("git --no-pager diff --no-index --exit-code $index $format") == 0;
114 # Is this binary the version we standardize on?
115 sub is_matching_clang_format_version
($$)
117 my ($clang_format, $version) = @_;
118 if (! -x
$clang_format)
123 return `'$clang_format' -version` =~ /^clang-format version $version(-\d+)? \(tags/;
128 # vim: set shiftwidth=4 softtabstop=4 expandtab: