LanguageServer/SyntaxChecker.pm

filter: Statementstmt 23.0% 35 / 152 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 47.6% 10 / 21 Podpod 0.0% 0 / 5 totaltotal 19.8% 45 / 227 CC 25 SCAR 46.5
CC
25
Cov
18%
background_checker
272.0
run_open3
44.1
run_win32
6.0
CRAP 104.9
« LanguageServer/Methods/workspace.pm ↑ Summary LanguageServer/Methods/textDocument.pm »
1 package Perl::LanguageServer::SyntaxChecker ;
2  
3 1 ▶use Moose::Role ;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Coro ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Coro::AIO ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Coro::Channel ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use AnyEvent::Util ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Encode ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #use Proc::FastSpawn;
14  
15 1 ▶no warnings 'uninitialized' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # ---------------------------------------------------------------------------
18  
19  
20 1 has 'infile' =>
21 (
22 is => 'rw',
23 isa => 'Str',
24 lazy_build => 1,
25 ) ;
26  
27 1 has 'outfile' =>
28 (
29 is => 'rw',
30 isa => 'Str',
31 lazy_build => 1,
32 ) ;
33  
34 has 'checker_channel' =>
35 (
36 is => 'ro',
37 isa => 'Coro::Channel',
38 0 ▶ default => sub { Coro::Channel -> new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 1 ) ;
40  
41 has 'checker2_channel' =>
42 (
43 is => 'ro',
44 isa => 'Coro::Channel',
45 0 ▶ default => sub { Coro::Channel -> new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 1 ) ;
47  
48 # ---------------------------------------------------------------------------
49  
50 sub _build_infile
51 0 ▶ {
Subroutine _build_infile: not called CC=1 SCAR=6.9
52 0 my ($fh, $filename) = File::Temp::tempfile();
53 0 close $fh ;
54  
55 0 return $filename ;
56 }
57  
58 # ---------------------------------------------------------------------------
59  
60 sub _build_outfile
61 0 ▶ {
Subroutine _build_outfile: not called CC=1 SCAR=6.9
62 0 my ($fh, $filename) = File::Temp::tempfile();
63 0 close $fh ;
64  
65 0 return $filename ;
66 }
67  
68  
69 # ---------------------------------------------------------------------------
70  
71 sub check_perl_syntax
72 0 ▶ {
Subroutine check_perl_syntax: not called CC=1 SCAR=6.9
Pod undocumented
73 0 my ($self, $workspace, $uri, $text) = @_ ;
74  
75 0 $self -> checker_channel -> put ([$uri, $text]) ;
76 }
77  
78  
79 # ---------------------------------------------------------------------------
80  
81 sub run_win32
82 0 ▶ {
Subroutine run_win32: not called CC=2 SCAR=17.9
Pod undocumented
83 0 my ($self, $text, $inc) = @_ ;
84  
85  
86 0 return (0, undef, undef) ; # disable for now on windows
87  
88 0 my $infile = $self -> infile ;
89 0 my $outfile = $self -> outfile ;
90  
91 0 print STDERR "infile=$infile outfile=$outfile\n" ;
92 0 ▶ my $ifh = aio_open ($infile, IO::AIO::O_WRONLY | IO::AIO::O_TRUNC | IO::AIO::O_CREAT, 0600) or die "open $infile failed ($!)" ;
Branch: 1 branch
BranchTrueFalseTotal
my $ifh = &aio_open($infile, 577, 384) or die "open $infile failed ($!)" 0 0 0
93 0 aio_write ($ifh, undef, undef, $text, 0) ;
94 0 aio_close ($ifh) ;
95  
96 0 print STDERR "run ", $self -> perlcmd . " -c @$inc $infile 2> $outfile", "\n" ;
97  
98 # use Win32::Process ;
99  
100 # my $cmd = $self -> perlcmd . " -c @$inc $infile" ;
101  
102 # print STDERR $cmd, "\n" ;
103  
104 # my $ProcessObj ;
105 0 my $rc ;
106 # Win32::Process::Create($ProcessObj,
107  
108 # $self -> perlcmd,
109 # $cmd,
110 # 0,
111 # NORMAL_PRIORITY_CLASS,
112 # ".");
113  
114 # print STDERR "wait\n" ;
115  
116 # $ProcessObj->Wait(5000) ;
117  
118 0 print STDERR "done\n" ;
119  
120 0 my $errout ;
121 my $out ;
122 0 aio_load ($outfile, $errout) ;
123 0 print STDERR "errout = $errout\n" ;
124  
125 0 return ($rc, $out, $errout) ;
126 }
127  
128  
129 # ---------------------------------------------------------------------------
130  
131 sub run_system
132 0 ▶ {
Subroutine run_system: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my ($self, $text, $inc) = @_ ;
134  
135 0 my $infile = $self -> infile ;
136 0 my $outfile = $self -> outfile ;
137  
138 0 local $SIG{CHLD} = 'DEFAULT' ;
139 0 local $SIG{PIPE} = 'DEFAULT' ;
140  
141 0 print STDERR "infile=$infile outfile=$outfile\n" ;
142 0 ▶ my $ifh = aio_open ($infile, IO::AIO::O_WRONLY | IO::AIO::O_TRUNC | IO::AIO::O_CREAT, 0600) or die "open $infile failed ($!)" ;
Branch: 1 branch
BranchTrueFalseTotal
my $ifh = &aio_open($infile, 577, 384) or die "open $infile failed ($!)" 0 0 0
143 0 aio_write ($ifh, undef, undef, $text, 0) ;
144 0 aio_close ($ifh) ;
145  
146 0 print STDERR "run ", $self -> perlcmd . " -c @$inc $infile 2> $outfile", "\n" ;
147 0 my $rc = system ($self -> perlcmd . " -c @$inc $infile 2> $outfile") ;
148 0 print STDERR "done\n" ;
149  
150 0 my $errout ;
151 my $out ;
152 0 aio_load ($outfile, $errout) ;
153 0 print STDERR "errout = $errout\n" ;
154  
155 0 return ($rc, $out, $errout) ;
156 }
157  
158 # ---------------------------------------------------------------------------
159  
160 sub run_open3
161 0 ▶ {
Subroutine run_open3: not called CC=7 SCAR=37.9
Pod undocumented
162 0 my ($self, $text, $inc) = @_ ;
163  
164 #return (0, undef, undef) ;
165  
166 0 my($wtr, $rdr, $err);
167  
168 0 require IPC::Open3 ;
169 0 ▶ use Symbol 'gensym'; $err = gensym;
Subroutine BEGIN: called CC=1 SCAR=0.2
170 0 ▶ $self -> logger ("open3\n") if ($Perl::LanguageServer::debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
171 0 ▶ my $pid = IPC::Open3::open3($wtr, $rdr, $err, $self -> perlcmd, '-c', @$inc) or die "Cannot run " . $self -> perlcmd ;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = IPC::Open3::open3($wtr, $rdr, $err, $self->perlcmd, "-c", @$inc) or die 'Cannot run ' . $self->perlcmd 0 0 0
172 0 ▶ $self -> logger ("write start pid=$pid\n") if ($Perl::LanguageServer::debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
173 0 syswrite ($wtr, $text . "\n__END__\n") ;
174 0 ▶ $self -> logger ("close start\n") if ($Perl::LanguageServer::debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
175 0 close ($wtr) ;
176 0 ▶ $self -> logger ("write done\n") if ($Perl::LanguageServer::debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
177  
178 0 my $out ;
179 0 my $errout = join ('', <$err>) ;
180 0 close $err ;
181 0 close $rdr ;
182 0 ▶ $self -> logger ("closed\n") if ($Perl::LanguageServer::debug2) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug2 0 0 0
183 0 waitpid( $pid, 0 );
184 0 my $rc = $? ;
185  
186 0 return ($rc, $out, $errout) ;
187 }
188  
189 # ---------------------------------------------------------------------------
190  
191 sub background_checker
192 0 ▶ {
Subroutine background_checker: not called CC=16 SCAR=56.1
Pod undocumented
193 0 my ($self, $server) = @_ ;
194  
195 async
196 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
197 0 my $channel1 = $self -> checker_channel ;
198 0 my $channel2 = $self -> checker2_channel ;
199  
200 0 my %timer ;
201 0 ▶ while (my $cmd = $channel1 -> get)
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd = $channel1->get) 0 0 0
202 {
203 0 my ($uri, $text) = @$cmd ;
204  
205 $timer{$uri} = AnyEvent->timer (after => 1.5, cb => sub
206 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
207 0 delete $timer{$uri} ;
208 0 $channel2 -> put($cmd) ;
209 0 }) ;
210 }
211  
212 0 } ;
213  
214 0 my $channel = $self -> checker2_channel ;
215  
216 0 ▶ while (my $cmd = $channel -> get)
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd = $channel->get) 0 0 0
217 {
218 0 my ($uri, $text) = @$cmd ;
219  
220 0 $text = eval { Encode::encode ('utf-8', $text) ; } ;
221 0 ▶ $self -> logger ($@) if ($@) ;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
222  
223 0 my $fn = $uri ;
224 0 $fn =~ s/^file:\/\/// ;
225 0 $fn = $self -> file_client2server ($fn) ;
226 0 $text = "local \$0; BEGIN { \$0 = '$fn'; if (\$INC{'FindBin.pm'}) { FindBin->again(); } }\n# line 1 \"$fn\"\n" . $text;
227  
228 0 my $ret ;
229 my $errout ;
230 0 my $out ;
231 0 my $inc = $self -> perlinc ;
232 0 my @inc ;
233 0 ▶ @inc = map { ('-I', $_)} @$inc if ($inc) ;
Branch: 1 branch
BranchTrueFalseTotal
if $inc 0 0 0
234  
235 0 my @syntax_options ;
236 0 ▶ if ($self -> use_taint_for_syntax_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_taint_for_syntax_check) 0 0 0
237 0 @syntax_options = ('-T') ;
238 }
239  
240 0 ▶ $self -> logger ("start perl @syntax_options -c @inc for $uri\n") if ($Perl::LanguageServer::debug1) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug1 0 0 0
241 0 ▶ if ($^O =~ /Win/)
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /Win/) { } 0 0 0
242 {
243 # ($ret, $out, $errout) = $self -> run_open3 ($text, \@inc) ;
244 0 ($ret, $out, $errout) = $self -> run_win32 ($text, \@inc) ;
245 }
246 else
247 {
248 0 $ret = run_cmd ([$self -> perlcmd, @syntax_options, '-c', @inc],
249 "<", \$text,
250 ">", \$out,
251 "2>", \$errout)
252 -> recv ;
253 }
254  
255 0 my $rc = $ret >> 8 ;
256 0 ▶ $self -> logger ("perl -c rc=$rc out=$out errout=$errout\n") if ($Perl::LanguageServer::debug1) ;
Branch: 1 branch
BranchTrueFalseTotal
if $Perl::LanguageServer::debug1 0 0 0
257  
258 0 my @messages ;
259 0 ▶ if ($rc != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($rc != 0) 0 0 0
260 {
261 0 my $line ;
262 0 my @lines = split /\n/, $errout ;
263 0 my $lineno = 0 ;
264 0 my $filename ;
265 0 my $lastline = 1 ;
266 0 my $msg ;
267 0 my $severity = 1 ;
268 0 foreach $line (@lines)
269 {
270 0 $line =~ s/\s*$// ;
271 #print STDERR $line, "\n" ;
272 0 ▶ next if ($line =~ /had compilation errors/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /had compilation errors/ 0 0 0
273 0 ▶ $filename = $1 if ($line =~ /at (.+?) line (\d+)[,.]/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /at (.+?) line (\d+)[,.]/ 0 0 0
274 #print STDERR "line = $lineno file=$filename fn=$fn\n" ;
275 0 ▶ $filename ||= $fn ;
Condition0%0/3
$filename ||= $fn
l!l&&r!l&&!r
000
MC/DC0%0/2
$filename ||= $fn
$filename $fn
Truth table0%0/3
A ||= B
A= $filenameB= $fn
ABresult
1X1
011
000
276 0 ▶ $lineno = $1 if ($line =~ / line (\d+)[,.]/) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / line (\d+)[,.]/ 0 0 0
277  
278 0 $msg .= $line ;
279 0 ▶ if ($lineno)
Branch: 1 branch
BranchTrueFalseTotal
if ($lineno) 0 0 0
280 {
281 0 ▶ push @messages, [$filename, $lineno, $severity, $msg] if ($msg) ;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
282 0 $lastline = $lineno ;
283 0 $lineno = 0 ;
284 0 $msg = '' ;
285 }
286 }
287 }
288  
289 0 $self -> add_diagnostic_messages ($server, $uri, 'perl syntax', \@messages) ;
290 }
291 }
292  
293 1 1;
294  
295 __END__
« LanguageServer/Methods/workspace.pm ↑ Summary LanguageServer/Methods/textDocument.pm »