package dglib;
use strict;
use warnings;
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(ispass parseLogFile);
use File::Basename;
use POSIX qw(mktime);
sub mkGroupPath {
my($root, $groups, @newgroups) = @_;
my $parent = $root;
my $fullname = "";
foreach my $group(@newgroups) {
$fullname .= "/" if $fullname;
$fullname .= $group;
if(exists($groups->{$fullname})) {
$parent = $groups->{$fullname};
} else {
my $newgroup = {name => $group, parent => $parent};
$groups->{$fullname} = $newgroup;
$parent->{testgroup} ||= [];
push @{$parent->{testgroup}}, $newgroup;
$parent = $newgroup;
}
}
return $parent;
}
sub parseLogFile($;$) {
my($logfile, $options) = @_;
$options ||= {};
my $gccdir = $options->{gccdir} || "";
my $return_diags = exists($options->{diagnostics}) ? $options->{diagnostics} : 1;
my $emit_fullname = exists($options->{fullname}) ? $options->{fullname} : 1;
my $is_gdb = 0;
my $gdbhack = "";
my %ret = (incomplete => 1, diagnostics => [], testgroup => []);
my(%testindex, %groupindex);
open(LOGFILE, $logfile) or die "Couldn't open log file $logfile: $!\n";
my($currgroup, $currtest, $lastrun);
$currgroup = \%ret;
my %monmap = (Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11);
$gccdir ||= "this will never match, or my name isn't Reginald St. Croix";
my $line = 1;
while(<LOGFILE>) {
chomp;
s/\x{d}$//; #^M
next if $_ eq "";
if(/^gcc version/) {
$ret{compiler} = $_;
} elsif(/^got a .* signal, interrupted by user /) {
$ret{incomplete} = 2;
} elsif(/^\s*=== gdb/) {
$is_gdb = 1;
$gdbhack = ".*";
} elsif(/^(Test Run By \S+ on|runtest completed at) ... (.{3}) (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) (\d{4})/) {
my $time = mktime($6, $5, $4, $3, $monmap{$2}, $7 - 1900);
if($1 eq "runtest completed at") {
$ret{end_time} = $time;
} else {
$ret{start_time} = $time;
}
} elsif(m<^Running (?!target )\Q$gccdir\E/?(\S+)> or m<^Running (?!target )\S*?((?:gcc|gdb|libstdc\+\+-v3)/testsuite/\S+)>) {
$lastrun = $1;
$lastrun =~ s!/[^/]*/\.\.!!; $currgroup = mkGroupPath(\%ret, \%groupindex, split(m!/!, $lastrun));
} elsif(/^Executing on (.*?):(.*)/) {
my($exectype, $execwhat) = ($1, $2);
next if $execwhat =~ /^dsymutil/;
$execwhat =~
s!.*?\s\Q$gccdir\E/?(\S+).*!$1! or
s!.*?/((?:gcc|gdb|libstdc\+\+-v3)/testsuite/\S+).*!$1! or
$exectype = "unix";
if($exectype eq "host" or !$currgroup) {
my $nogroup = 0;
if($execwhat =~ / /) {
$execwhat = dirname($lastrun) . "/" . basename($execwhat) if $lastrun and $execwhat;
$execwhat =~ s/\s.*//;
if($execwhat =~ m!/(gcc|c\+\+)$!) {
undef $currtest;
undef $currgroup;
$nogroup = 1;
}
}
if(!$nogroup) {
undef $currtest;
$execwhat =~ s!/[^/]*/\.\.!!;
if($lastrun) {
my $lastbase = dirname($lastrun);
my $basegroup = $execwhat;
$basegroup =~ s!^\Q$lastbase\E/!!;
$execwhat = "$lastrun/$basegroup";
}
$currgroup = mkGroupPath(\%ret, \%groupindex, split(m!/!, $execwhat));
}
} else {
$currtest = {};
}
} elsif(/^
if($ret{incomplete} == 2) {
$ret{incomplete} = 1;
} else {
$ret{incomplete} = 0;
}
} elsif(/^testcase .* completed/) {
undef $currtest;
undef $currgroup;
} elsif(/^$gdbhack(FAIL|PASS|UNRESOLVED|UNSUPPORTED|UNTESTED|XFAIL|XPASS|KFAIL|KPASS): (.*)/) {
undef $currtest if $currtest->{name};
my $phase = ""; my($test, $result) = ($2, $1);
if($test =~ /^(.*) compile\s*$/) {
$test = "$1 compile,link,execute";
$phase = "compile";
} elsif($test =~ /^(.*)-(.*) (link|execute)\s*$/) {
$test = "$1 compile,link,execute";
if($3 eq "link") {
$phase = "link";
} else {
$phase = "execute";
}
} elsif($test =~ /(compile|compilation|execute|execution)/) {
my $phasematch = $1;
if($test =~ /^com/) {
$phase = "compile";
} else {
$phase = "execute";
}
$test =~ s!\Q$phasematch\E!compile,execute!;
}
my $failwhy = "";
$test =~ s/ gcov failed: (.*)// and $failwhy = $1;
if(!$is_gdb and $test !~ /scan-assembler/ and $test =~ s/:\s*(.+)//) {
$failwhy = $1;
}
$test =~ s/\s*$//;
$test =~ s/^\s*$//;
my $basetest = $test;
$basetest =~ s/:? .*//;
if(exists($testindex{$test}) and !$testindex{$test}->{pseudotest}) {
$currtest = $testindex{$test};
if(ispass($currtest->{result})) {
$currtest->{result} = $result;
$currtest->{detail} = "$phase phase";
$currtest->{detail} .= "; $failwhy" if $failwhy;
}
} else {
$currtest ||= $testindex{$test} || {};
$currtest->{name} = basename($test);
if($emit_fullname) {
$currtest->{fullname} = ($currgroup->{name} || dirname($test)) . "/$currtest->{name}";
}
my $grpname = $currgroup->{name} || "";
$currtest->{name} =~ s/^\s*\Q$grpname\E\s*//;
$currtest->{name} =~ s/^: // if $is_gdb;
my $orig_currgroup = $currgroup;
if(!$currtest->{name}) {
$currtest->{name} = $grpname;
$currgroup = $currgroup->{parent};
$grpname = $currgroup->{name} || "";
}
$currtest->{result} = $result;
if($phase and $failwhy) {
$currtest->{detail} = "$phase phase; $failwhy" if $phase;
} elsif($phase) {
$currtest->{detail} = "$phase phase";
} elsif($failwhy) {
$currtest->{detail} = $failwhy;
}
$currgroup->{test} ||= [];
push @{$currgroup->{test}}, $currtest;
$testindex{$test} = $currtest;
$currgroup = $orig_currgroup;
if($basetest ne $test) {
if(!exists($testindex{$basetest}) ) {
my $btbase = basename($basetest);
$testindex{$basetest} = {
name => $btbase,
result => $result,
pseudotest => 1,
fullname => $btbase
};
if($emit_fullname) {
$testindex{basetest}->{fullname} = ($currgroup->{name} || dirname($basetest)) . "/$btbase";
}
push @{$currgroup->{parent}->{test}}, $testindex{$basetest};
} else {
$testindex{$basetest}->{result} = $result if !ispass($result);
}
}
}
} elsif(/^\s+=== .* Summary ===\s*$/) {
undef $currgroup;
undef $currtest;
}
my $severity;
if(/^(ERROR|WARNING|NOTE): (.*)/) {
$severity = $1;
my $message = $2;
if($message eq "program timed out.") {
$currtest->{result} = "TIMEDOUT";
} elsif(
$message =~ /can't read "(HOSTCC|libiconv)": no such variable/ or
$message =~ /no files matched glob pattern/ or
$message =~ /error executing dg-final: .*: no such file/
) {
$severity = "NOTE";
}
} else {
$severity = "logline";
}
if($return_diags) {
my $dobj;
if($currtest) {
$currtest->{diagnostics} ||= [];
$dobj = $currtest->{diagnostics};
} elsif($currgroup) {
$currgroup->{diagnostics} ||= [];
$dobj = $currgroup->{diagnostics};
} else {
$dobj = $ret{diagnostics};
}
push @$dobj, {message => $_, severity => $severity, line => $line};
}
} continue {
$line++;
}
close LOGFILE;
return %ret;
}
# Split a test into testdivs
sub splitTest($$) {
my($root, $test) = @_;
$test->{fullname} =~ /^(\S+)\s*(.*)/;
my($path, $descriptor) = ($1, $2);
my @nodes = split(m!/!, $path);
push @nodes, $descriptor if $descriptor;
my $lastnode = pop @nodes;
my $hash = $root;
foreach (@nodes) {
$hash->{testdiv} ||= {};
$hash = $hash->{testdiv}->{$_} ||= {};
}
$hash->{test} ||= {};
$hash->{test}->{$lastnode} = $test;
}
# ==== Comparison ====
sub ispass($) {
my $result = shift;
if($result eq "PASS" or $result eq "XFAIL" or $result eq "KFAIL") {
return 1;
} else {
return 0;
}
}
1;