Skip to content

Commit db50e79

Browse files
authored
tools: implement cgen tag for Markdown examples in v check-md (#13332)
1 parent b34860e commit db50e79

File tree

5 files changed

+86
-51
lines changed

5 files changed

+86
-51
lines changed

cmd/tools/vcheck-md.v

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
hide_warnings = '-hide-warnings' in os.args || '-w' in os.args
2222
show_progress = os.getenv('GITHUB_JOB') == '' && '-silent' !in os.args
2323
non_option_args = cmdline.only_non_options(os.args[2..])
24+
is_verbose = os.getenv('VERBOSE') != ''
2425
)
2526

2627
struct CheckResult {
@@ -75,7 +76,7 @@ fn main() {
7576
res += mdfile.check()
7677
}
7778
if res.errors == 0 && show_progress {
78-
term.clear_previous_line()
79+
clear_previous_line()
7980
}
8081
if res.warnings > 0 || res.errors > 0 || res.oks > 0 {
8182
println('\nWarnings: $res.warnings | Errors: $res.errors | OKs: $res.oks')
@@ -131,9 +132,7 @@ fn eline(file_path string, lnumber int, column int, message string) string {
131132
return btext('$file_path:${lnumber + 1}:${column + 1}:') + btext(rtext(' error: $message'))
132133
}
133134

134-
const (
135-
default_command = 'compile'
136-
)
135+
const default_command = 'compile'
137136

138137
struct VCodeExample {
139138
mut:
@@ -160,7 +159,7 @@ mut:
160159

161160
fn (mut f MDFile) progress(message string) {
162161
if show_progress {
163-
term.clear_previous_line()
162+
clear_previous_line()
164163
println('File: ${f.path:-30s}, Lines: ${f.lines.len:5}, $message')
165164
}
166165
}
@@ -394,6 +393,7 @@ fn (mut f MDFile) debug() {
394393
}
395394

396395
fn cmdexecute(cmd string) int {
396+
verbose_println(cmd)
397397
res := os.execute(cmd)
398398
if res.exit_code < 0 {
399399
return 1
@@ -405,6 +405,7 @@ fn cmdexecute(cmd string) int {
405405
}
406406

407407
fn silent_cmdexecute(cmd string) int {
408+
verbose_println(cmd)
408409
res := os.execute(cmd)
409410
return res.exit_code
410411
}
@@ -426,6 +427,7 @@ fn (mut f MDFile) check_examples() CheckResult {
426427
}
427428
fname := os.base(f.path).replace('.md', '_md')
428429
uid := rand.ulid()
430+
cfile := os.join_path(os.temp_dir(), '${uid}.c')
429431
vfile := os.join_path(os.temp_dir(), 'check_${fname}_example_${e.sline}__${e.eline}__${uid}.v')
430432
mut should_cleanup_vfile := true
431433
// eprintln('>>> checking example $vfile ...')
@@ -438,8 +440,7 @@ fn (mut f MDFile) check_examples() CheckResult {
438440
fmt_res := if nofmt { 0 } else { get_fmt_exit_code(vfile, vexe) }
439441
match command {
440442
'compile' {
441-
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -o x.c ${os.quoted_path(vfile)}')
442-
os.rm('x.c') or {}
443+
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors ${os.quoted_path(vfile)}')
443444
if res != 0 || fmt_res != 0 {
444445
if res != 0 {
445446
eprintln(eline(f.path, e.sline, 0, 'example failed to compile'))
@@ -454,9 +455,26 @@ fn (mut f MDFile) check_examples() CheckResult {
454455
}
455456
oks++
456457
}
458+
'cgen' {
459+
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -o ${os.quoted_path(cfile)} ${os.quoted_path(vfile)}')
460+
os.rm(cfile) or {}
461+
if res != 0 || fmt_res != 0 {
462+
if res != 0 {
463+
eprintln(eline(f.path, e.sline, 0, 'example failed to generate C code'))
464+
}
465+
if fmt_res != 0 {
466+
eprintln(eline(f.path, e.sline, 0, 'example is not formatted'))
467+
}
468+
eprintln(vcontent)
469+
should_cleanup_vfile = false
470+
errors++
471+
continue
472+
}
473+
oks++
474+
}
457475
'globals' {
458-
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -enable-globals -o x.c ${os.quoted_path(vfile)}')
459-
os.rm('x.c') or {}
476+
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -enable-globals -o ${os.quoted_path(cfile)} ${os.quoted_path(vfile)}')
477+
os.rm(cfile) or {}
460478
if res != 0 || fmt_res != 0 {
461479
if res != 0 {
462480
eprintln(eline(f.path, e.sline, 0, '`example failed to compile with -enable-globals'))
@@ -472,7 +490,8 @@ fn (mut f MDFile) check_examples() CheckResult {
472490
oks++
473491
}
474492
'live' {
475-
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -live -o x.c ${os.quoted_path(vfile)}')
493+
res := cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -live -o ${os.quoted_path(cfile)} ${os.quoted_path(vfile)}')
494+
os.rm(cfile) or {}
476495
if res != 0 || fmt_res != 0 {
477496
if res != 0 {
478497
eprintln(eline(f.path, e.sline, 0, 'example failed to compile with -live'))
@@ -488,8 +507,8 @@ fn (mut f MDFile) check_examples() CheckResult {
488507
oks++
489508
}
490509
'failcompile' {
491-
res := silent_cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -o x.c ${os.quoted_path(vfile)}')
492-
os.rm('x.c') or {}
510+
res := silent_cmdexecute('${os.quoted_path(vexe)} -w -Wfatal-errors -o ${os.quoted_path(cfile)} ${os.quoted_path(vfile)}')
511+
os.rm(cfile) or {}
493512
if res == 0 || fmt_res != 0 {
494513
if res == 0 {
495514
eprintln(eline(f.path, e.sline, 0, '`failcompile` example compiled'))
@@ -533,7 +552,7 @@ fn (mut f MDFile) check_examples() CheckResult {
533552
}
534553
'nofmt' {}
535554
else {
536-
eprintln(eline(f.path, e.sline, 0, 'unrecognized command: "$command", use one of: wip/ignore/compile/failcompile/oksyntax/badsyntax'))
555+
eprintln(eline(f.path, e.sline, 0, 'unrecognized command: "$command", use one of: wip/ignore/compile/cgen/failcompile/oksyntax/badsyntax/nofmt'))
537556
should_cleanup_vfile = false
538557
errors++
539558
}
@@ -548,3 +567,16 @@ fn (mut f MDFile) check_examples() CheckResult {
548567
oks: oks
549568
}
550569
}
570+
571+
fn verbose_println(message string) {
572+
if is_verbose {
573+
println(message)
574+
}
575+
}
576+
577+
fn clear_previous_line() {
578+
if is_verbose {
579+
return
580+
}
581+
term.clear_previous_line()
582+
}

cmd/v/help/check-md.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Flags:
1313
NB: There are several special keywords, which you can put after the code fences for v.
1414
These are:
1515
compile - Default, can be omitted. The example will be compiled and formatting is verified.
16+
cgen - The example produces C code, which may not be compilable (when external libs are not installed). Formatting is verified.
1617
live - Compile hot reload examples with the ´-live´ flag set and verify formatting.
1718
ignore - Ignore the example, useful for examples that just use the syntax highlighting
1819
failcompile - Known failing compilation. Useful for examples demonstrating compiler errors.

doc/docs.md

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ For more details and troubleshooting, please visit the [vab GitHub repository](h
163163

164164
<!--
165165
NB: there are several special keywords, which you can put after the code fences for v:
166-
compile, live, ignore, failcompile, oksyntax, badsyntax, wip, nofmt
166+
compile, cgen, live, ignore, failcompile, oksyntax, badsyntax, wip, nofmt
167167
For more details, do: `v check-md`
168168
-->
169169

@@ -1393,7 +1393,7 @@ println(s)
13931393
You can check the current type of a sum type using `is` and its negated form `!is`.
13941394

13951395
You can do it either in an `if`:
1396-
```v
1396+
```v cgen
13971397
struct Abc {
13981398
val string
13991399
}
@@ -5102,40 +5102,42 @@ For all supported options check the latest help:
51025102

51035103
#### `$if` condition
51045104
```v
5105-
// Support for multiple conditions in one branch
5106-
$if ios || android {
5107-
println('Running on a mobile device!')
5108-
}
5109-
$if linux && x64 {
5110-
println('64-bit Linux.')
5111-
}
5112-
// Usage as expression
5113-
os := $if windows { 'Windows' } $else { 'UNIX' }
5114-
println('Using $os')
5115-
// $else-$if branches
5116-
$if tinyc {
5117-
println('tinyc')
5118-
} $else $if clang {
5119-
println('clang')
5120-
} $else $if gcc {
5121-
println('gcc')
5122-
} $else {
5123-
println('different compiler')
5124-
}
5125-
$if test {
5126-
println('testing')
5127-
}
5128-
// v -cg ...
5129-
$if debug {
5130-
println('debugging')
5131-
}
5132-
// v -prod ...
5133-
$if prod {
5134-
println('production build')
5135-
}
5136-
// v -d option ...
5137-
$if option ? {
5138-
println('custom option')
5105+
fn main() {
5106+
// Support for multiple conditions in one branch
5107+
$if ios || android {
5108+
println('Running on a mobile device!')
5109+
}
5110+
$if linux && x64 {
5111+
println('64-bit Linux.')
5112+
}
5113+
// Usage as expression
5114+
os := $if windows { 'Windows' } $else { 'UNIX' }
5115+
println('Using $os')
5116+
// $else-$if branches
5117+
$if tinyc {
5118+
println('tinyc')
5119+
} $else $if clang {
5120+
println('clang')
5121+
} $else $if gcc {
5122+
println('gcc')
5123+
} $else {
5124+
println('different compiler')
5125+
}
5126+
$if test {
5127+
println('testing')
5128+
}
5129+
// v -cg ...
5130+
$if debug {
5131+
println('debugging')
5132+
}
5133+
// v -prod ...
5134+
$if prod {
5135+
println('production build')
5136+
}
5137+
// v -d option ...
5138+
$if option ? {
5139+
println('custom option')
5140+
}
51395141
}
51405142
```
51415143

vlib/gg/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ user's keyboard/mouse input.
77

88
## Example:
99

10-
```v
10+
```v cgen
1111
module main
1212
1313
import gg

vlib/sokol/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Each `.h` file in the sokol source code is well-documented as can be seen here:
1111

1212
## Example from `@VROOTDIR/examples/sokol/sounds/simple_sin_tones.v`:
1313

14-
```v
14+
```v cgen
1515
import time
1616
import math
1717
import sokol.audio

0 commit comments

Comments
 (0)