summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/tag-major-upgrade
blob: 9e56e7f5484e20939926f16d8cf6de1a5d2bbb1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/perl -w
use Getopt::Std;
use strict;

our ($opt_n, $opt_t);
getopts('nt:');
my $dry_run = $opt_n;
my $tag = $opt_t;
my $any_changed = 0;

sub fix_tag($$$$$)
{
    my ($op, $ver, $body, $else, $full) = @_;
    my $matches;

    # Only obsolete tags
    return $full unless $ver < $tag;

    if ($op eq '==')
    {
        $matches = $tag == $ver;
    }
    elsif ($op eq '!=')
    {
        $matches = $tag != $ver;
    }
    elsif ($op eq '<=')
    {
        $matches = $tag <= $ver;
    }
    elsif ($op eq '<')
    {
        $matches = $tag < $ver;
    }
    elsif ($op eq '>=')
    {
        $matches = $tag >= $ver;
    }
    elsif ($op eq '>')
    {
        $matches = $tag > $ver;
    }
    die "Unknown operator '$op'" unless defined $matches;

    return $matches      ? $body
         : defined $else ? $else
                         : "";
}

if (!defined $tag)
{
    my $tagf = `git ls-files '*tag-version.h'`;
    chomp $tagf;
    die "Too many matches for tag-version.h\n" if $tagf =~ /\n/;

    open T, "<", $tagf or die "Could not open $tagf: $!\n";
    while (<T>)
    {
        /^#define\s+TAG_MAJOR_VERSION\s+(\d+)/ and $tag = $1;
    }
}

die "Could not determine version tag\n" unless defined $tag;

my @files = @ARGV;
undef $/;
unless (@files)
{
    @files = (grep /\.(cc|h)$/ && !/(^|\/)prebuilt\//, split /\n/, `git ls-files`);
}

for my $f (@files)
{
    open F, "<", $f or die "Can't read $f\n";
    my $old = $_ = <F>;
    close F;

    s/# Full match in $1
      (
        # #if line. Comparison operator in $2, tag in $3
        ^\s* \# \s* if \s+ TAG_MAJOR_VERSION \s* (!=|==|>=|>|<=|<) \s* (\d+) \n

        # body of #if in $4.  We don't handle #if TAG_MAJOR_VERSION blocks
        # that contain nested #ifs.
        ((?: (?! \s* \# \s* (?:if|elif|else|endif)) .* \n)+)

        # optional #else, body in $5
        (?:
            \s* \# \s* else \n
            ((?: (?! \s* \# \s* (?:if|elif|else|endif)) .* \n)+)
        )?

        \s* \# \s* endif \n
      )
    /fix_tag($2, $3, $4, $5, $1)/gexm;

    if ($old ne $_)
    {
        print "$f\n";
        $any_changed = 1;
        if (!$dry_run)
        {
            open F, ">", $f or die "Can't write $f\n";
            print F;
            close F;
        }
    }
}

exit 1 if ($dry_run and $any_changed);