Skip to content

Commit 652881c

Browse files
committed
all: vls mode fixes and improvements; v -json-errors flag
1 parent a11de72 commit 652881c

File tree

7 files changed

+104
-22
lines changed

7 files changed

+104
-22
lines changed

vlib/os/os_nix.c.v

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,20 @@ const stderr_value = 2
2929
// (Must be realized in Syscall) (Must be specified)
3030
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
3131
pub const s_ifmt = 0xF000 // type of file
32-
3332
pub const s_ifdir = 0x4000 // directory
34-
3533
pub const s_ifreg = 0x8000 // regular file
36-
3734
pub const s_iflnk = 0xa000 // link
38-
3935
pub const s_isuid = 0o4000 // SUID
40-
4136
pub const s_isgid = 0o2000 // SGID
42-
4337
pub const s_isvtx = 0o1000 // Sticky
44-
4538
pub const s_irusr = 0o0400 // Read by owner
46-
4739
pub const s_iwusr = 0o0200 // Write by owner
48-
4940
pub const s_ixusr = 0o0100 // Execute by owner
50-
5141
pub const s_irgrp = 0o0040 // Read by group
52-
5342
pub const s_iwgrp = 0o0020 // Write by group
54-
5543
pub const s_ixgrp = 0o0010 // Execute by group
56-
5744
pub const s_iroth = 0o0004 // Read by others
58-
5945
pub const s_iwoth = 0o0002 // Write by others
60-
6146
pub const s_ixoth = 0o0001
6247

6348
fn C.utime(&char, &C.utimbuf) int

vlib/os/util/util.v

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module util
2+
3+
import os
4+
5+
// TODO `select` doesn't work with time.Duration for some reason
6+
pub fn execute_with_timeout(cmd string, timeout i64) ?os.Result {
7+
ch := chan os.Result{cap: 1}
8+
spawn fn [cmd] (c chan os.Result) {
9+
res := os.execute(cmd)
10+
c <- res
11+
}(ch)
12+
select {
13+
a := <-ch {
14+
return a
15+
}
16+
// timeout {
17+
// 1000 * time.millisecond {
18+
// timeout * time.millisecond {
19+
timeout * 1_000_000 {
20+
return none
21+
}
22+
}
23+
return os.Result{}
24+
}

vlib/v/builder/builder.v

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import v.markused
1414
import v.depgraph
1515
import v.callgraph
1616
import v.dotgraph
17+
// import x.json2
1718

1819
pub struct Builder {
1920
pub:
@@ -475,7 +476,8 @@ pub fn (b &Builder) show_total_warns_and_errors_stats() {
475476
println('checker summary: ${estring} V errors, ${wstring} V warnings, ${nstring} V notices')
476477
}
477478
}
478-
if b.checker.nr_errors > 0 && b.pref.path.ends_with('.v') && os.is_file(b.pref.path) {
479+
if !b.pref.is_vls && b.checker.nr_errors > 0 && b.pref.path.ends_with('.v')
480+
&& os.is_file(b.pref.path) {
479481
if b.checker.errors.any(it.message.starts_with('unknown ')) {
480482
// Sometimes users try to `v main.v`, when they have several .v files in their project.
481483
// Then, they encounter puzzling errors about missing or unknown types. In this case,
@@ -519,16 +521,32 @@ pub fn (mut b Builder) print_warnings_and_errors() {
519521
}
520522
}
521523

524+
mut json_errors := []util.JsonError{}
522525
for file in b.parsed_files {
523526
for err in file.errors {
524527
kind := if b.pref.is_verbose {
525528
'${err.reporter} error #${b.nr_errors}:'
526529
} else {
527530
'error:'
528531
}
529-
util.show_compiler_message(kind, err.CompilerMessage)
532+
533+
if b.pref.json_errors {
534+
json_errors << util.JsonError{
535+
message: err.message
536+
path: err.file_path
537+
line_nr: err.pos.line_nr + 1
538+
col: err.pos.col + 1
539+
}
540+
// util.print_json_error(kind, err.CompilerMessage)
541+
} else {
542+
util.show_compiler_message(kind, err.CompilerMessage)
543+
}
530544
}
531545
}
546+
if b.pref.json_errors {
547+
util.print_json_errors(json_errors)
548+
// eprintln(json2.encode_pretty(json_errors))
549+
}
532550

533551
if !b.pref.skip_warnings {
534552
for file in b.parsed_files {

vlib/v/checker/checker.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,10 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
18811881
}
18821882
} else {
18831883
if unknown_field_msg == '' {
1884+
if field_name == '' && c.pref.is_vls {
1885+
// VLS will often have `foo.`, skip the no field error
1886+
return ast.void_type
1887+
}
18841888
unknown_field_msg = 'type `${sym.name}` has no field named `${field_name}`'
18851889
}
18861890
if sym.info is ast.Struct {

vlib/v/parser/parser.v

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,12 @@ pub fn parse_file(path string, mut table ast.Table, comments_mode scanner.Commen
249249
eprintln('> ${@MOD}.${@FN} comments_mode: ${comments_mode:-20} | path: ${path}')
250250
}
251251
mut p := Parser{
252-
scanner: scanner.new_scanner_file(path, comments_mode, pref_) or { panic(err) }
253-
table: table
254-
pref: pref_
252+
scanner: scanner.new_scanner_file(path, comments_mode, pref_) or { panic(err) }
253+
table: table
254+
pref: pref_
255+
// Only set vls mode if it's the file the user requested via `v -vls-mode file.v`
256+
// Otherwise we'd be parsing entire stdlib in vls mode
257+
is_vls: pref_.is_vls && path == pref_.path
255258
scope: &ast.Scope{
256259
start_pos: 0
257260
parent: table.global_scope

vlib/v/pref/pref.v

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ pub mut:
257257
// forwards compatibility settings:
258258
relaxed_gcc14 bool = true // turn on the generated pragmas, that make gcc versions > 14 a lot less pedantic. The default is to have those pragmas in the generated C output, so that gcc-14 can be used on Arch etc.
259259
//
260-
subsystem Subsystem // the type of the window app, that is going to be generated; has no effect on !windows
261-
is_vls bool
260+
subsystem Subsystem // the type of the window app, that is going to be generated; has no effect on !windows
261+
is_vls bool
262+
json_errors bool // -json-errors, for VLS and other tools
262263
}
263264

264265
pub struct LineInfo {
@@ -406,6 +407,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
406407
'-check' {
407408
res.check_only = true
408409
}
410+
'-vls-mode' {
411+
res.is_vls = true
412+
}
409413
'-?', '-h', '-help', '--help' {
410414
// Note: help is *very important*, just respond to all variations:
411415
res.is_help = true
@@ -561,6 +565,9 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
561565
'-repl' {
562566
res.is_repl = true
563567
}
568+
'-json-errors' {
569+
res.json_errors = true
570+
}
564571
'-live' {
565572
res.is_livemain = true
566573
}

vlib/v/util/errors.v

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,44 @@ pub fn show_compiler_message(kind string, err errors.CompilerMessage) {
210210
eprintln(bold('Details: ') + color('details', err.details))
211211
}
212212
}
213+
214+
pub struct JsonError {
215+
pub:
216+
path string
217+
message string
218+
line_nr int
219+
col int
220+
len int
221+
}
222+
223+
pub fn print_json_errors(errs []JsonError) {
224+
// Can't import x.json2 or json, so have to manually generate json
225+
eprintln('[')
226+
for i, e in errs {
227+
msg := e.message.replace('"', '\\"').replace('\n', '\\n')
228+
eprintln('{
229+
"path":"${e.path}",
230+
"message":"${msg}",
231+
"line_nr":${e.line_nr},
232+
"col":${e.col},
233+
"len":${e.len}
234+
}')
235+
if i < errs.len - 1 {
236+
eprintln(',')
237+
}
238+
}
239+
eprintln(']')
240+
}
241+
242+
/*
243+
pub fn print_json_error(kind string, err errors.CompilerMessage) {
244+
e := JsonError{
245+
message: err.message
246+
path: err.file_path
247+
line_nr: err.pos.line_nr + 1
248+
col: err.pos.col + 1
249+
len: err.pos.len
250+
}
251+
eprintln(json2.encode_pretty(e))
252+
}
253+
*/

0 commit comments

Comments
 (0)