Resolves: tdf#149408 inspector crash with a writer OLE inside calc
[LibreOffice.git] / solenv / clang-format / ClangFormat.pm
blob30f3816231bf855556d37eaf08c1913898d64487
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/.
7 package ClangFormat;
9 use strict;
10 use warnings;
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.
15 sub get_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>)
25 chomp $line;
26 $excludelist_names{$line} = 1;
30 return \%excludelist_names;
33 # Writes the excludelist.
34 # The single argument is a reference to an array.
35 sub set_excludelist
37 my @filenames = @{$_[0]};
38 open my $fh, ">", "solenv/clang-format/excludelist" or die $!;
39 print $fh "$_\n" for @filenames;
40 close $fh;
43 # Returns the clang-format version used of style enforcement.
44 sub get_wanted_version()
46 return "5.0.0";
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()
53 return "/opt/lo/bin";
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.
64 sub find()
66 my $version = get_wanted_version();
67 my $opt_lo = get_own_directory();
68 my $clang_format;
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";
79 last;
83 else
85 $clang_format = $ENV{CLANG_FORMAT};
88 if ($^O eq "cygwin" && defined($clang_format))
90 $clang_format = `cygpath -m '$clang_format'`;
91 chomp $clang_format;
94 return $clang_format;
97 # Diffs the original and the formatted version of a single file from the index.
98 sub check_style($$)
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;
107 unlink($index);
108 unlink($format);
109 return $ret;
112 # Private functions.
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)
120 return 0;
123 return `'$clang_format' -version` =~ /^clang-format version $version(-\d+)? \(tags/;
128 # vim: set shiftwidth=4 softtabstop=4 expandtab: