From ac6fad6a639c48f95a78d48862666fe2fdc3f829 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 24 Oct 2020 10:13:58 +0300 Subject: [PATCH] checker: fix comptime custom defines usages, add tests --- vlib/v/checker/checker.v | 4 +- ...tom_comptime_define_error.mysymbol.run.out | 2 + .../tests/custom_comptime_define_error.out | 7 +++ .../tests/custom_comptime_define_error.vv | 10 ++++ ...om_comptime_define_if_flag.mydebug.run.out | 4 ++ ...om_comptime_define_if_flag.nodebug.run.out | 2 + .../tests/custom_comptime_define_if_flag.out | 0 .../tests/custom_comptime_define_if_flag.vv | 13 +++++ vlib/v/compiler_errors_test.v | 57 ++++++++++--------- 9 files changed, 71 insertions(+), 28 deletions(-) create mode 100644 vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out create mode 100644 vlib/v/checker/tests/custom_comptime_define_error.out create mode 100644 vlib/v/checker/tests/custom_comptime_define_error.vv create mode 100644 vlib/v/checker/tests/custom_comptime_define_if_flag.mydebug.run.out create mode 100644 vlib/v/checker/tests/custom_comptime_define_if_flag.nodebug.run.out create mode 100644 vlib/v/checker/tests/custom_comptime_define_if_flag.out create mode 100644 vlib/v/checker/tests/custom_comptime_define_if_flag.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ea6e80814c..499a544287 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3615,7 +3615,9 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool { else { return false } } } else { - c.error('unknown \$if value', pos) + if cond.name !in c.pref.compile_defines_all { + c.error('unknown \$if value', pos) + } } } else { diff --git a/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out b/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out new file mode 100644 index 0000000000..97a3e70c2d --- /dev/null +++ b/vlib/v/checker/tests/custom_comptime_define_error.mysymbol.run.out @@ -0,0 +1,2 @@ +optional compitme define works +non optional comptime define works diff --git a/vlib/v/checker/tests/custom_comptime_define_error.out b/vlib/v/checker/tests/custom_comptime_define_error.out new file mode 100644 index 0000000000..156b08e298 --- /dev/null +++ b/vlib/v/checker/tests/custom_comptime_define_error.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/custom_comptime_define_error.vv:6:9: error: unknown $if value + 4 | println('optional compitme define works') + 5 | } + 6 | $if mysymbol { + | ~~~~~~~~~~~~ + 7 | // this will produce a checker error when `-d mysymbol` is not given on the CLI + 8 | println('non optional comptime define works') diff --git a/vlib/v/checker/tests/custom_comptime_define_error.vv b/vlib/v/checker/tests/custom_comptime_define_error.vv new file mode 100644 index 0000000000..34f99676a7 --- /dev/null +++ b/vlib/v/checker/tests/custom_comptime_define_error.vv @@ -0,0 +1,10 @@ +fn main() { + $if mysymbol? { + // this should not produce checker errors, but will print only with `-d mysymbol` + println('optional compitme define works') + } + $if mysymbol { + // this will produce a checker error when `-d mysymbol` is not given on the CLI + println('non optional comptime define works') + } +} diff --git a/vlib/v/checker/tests/custom_comptime_define_if_flag.mydebug.run.out b/vlib/v/checker/tests/custom_comptime_define_if_flag.mydebug.run.out new file mode 100644 index 0000000000..7cf4d9295b --- /dev/null +++ b/vlib/v/checker/tests/custom_comptime_define_if_flag.mydebug.run.out @@ -0,0 +1,4 @@ +start +message: verbose message +message: debugging system +end diff --git a/vlib/v/checker/tests/custom_comptime_define_if_flag.nodebug.run.out b/vlib/v/checker/tests/custom_comptime_define_if_flag.nodebug.run.out new file mode 100644 index 0000000000..5d0fb3b2d2 --- /dev/null +++ b/vlib/v/checker/tests/custom_comptime_define_if_flag.nodebug.run.out @@ -0,0 +1,2 @@ +start +end diff --git a/vlib/v/checker/tests/custom_comptime_define_if_flag.out b/vlib/v/checker/tests/custom_comptime_define_if_flag.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vlib/v/checker/tests/custom_comptime_define_if_flag.vv b/vlib/v/checker/tests/custom_comptime_define_if_flag.vv new file mode 100644 index 0000000000..34bb3c7de0 --- /dev/null +++ b/vlib/v/checker/tests/custom_comptime_define_if_flag.vv @@ -0,0 +1,13 @@ +// Calls to edebug/1 should be executed only when `-d mydebug` is passed on the CLI +// Otherwise they will not be present *at all* in the generated code. +[if mydebug] +fn edebug(message string) { + println('message: $message') +} + +fn main() { + println('start') + edebug('verbose message') + edebug('debugging system') + println('end') +} diff --git a/vlib/v/compiler_errors_test.v b/vlib/v/compiler_errors_test.v index d8dd988695..20ab4a7010 100644 --- a/vlib/v/compiler_errors_test.v +++ b/vlib/v/compiler_errors_test.v @@ -11,6 +11,7 @@ const ( skip_files = [ 'vlib/v/checker/tests/return_missing_comp_if.vv', 'vlib/v/checker/tests/return_missing_comp_if_nested.vv', + 'vlib/v/checker/tests/custom_comptime_define_if_flag.vv', ] ) @@ -33,36 +34,37 @@ fn test_all() { vexe := os.getenv('VEXE') vroot := os.dir(vexe) os.chdir(vroot) - classic_dir := 'vlib/v/checker/tests' - classic_tests := get_tests_in_dir(classic_dir, false) - global_dir := '$classic_dir/globals' - global_tests := get_tests_in_dir(global_dir, false) - module_dir := '$classic_dir/modules' - module_tests := get_tests_in_dir(module_dir, true) - run_dir := '$classic_dir/run' - run_tests := get_tests_in_dir(run_dir, false) + checker_dir := 'vlib/v/checker/tests' parser_dir := 'vlib/v/parser/tests' + module_dir := '$checker_dir/modules' + global_dir := '$checker_dir/globals' + run_dir := '$checker_dir/run' + // + checker_tests := get_tests_in_dir(checker_dir, false) parser_tests := get_tests_in_dir(parser_dir, false) - // -prod so that warns are errors + global_tests := get_tests_in_dir(global_dir, false) + module_tests := get_tests_in_dir(module_dir, true) + run_tests := get_tests_in_dir(run_dir, false) + // -prod is used for the parser and checker tests, so that warns are errors mut tasks := []TaskDescription{} - tasks << new_tasks(vexe, classic_dir, '-prod', '.out', classic_tests, false) - tasks << - new_tasks(vexe, global_dir, '--enable-globals', '.out', global_tests, false) - tasks << - new_tasks(vexe, classic_dir, '--enable-globals run', '.run.out', ['globals_error.vv'], false) - tasks << new_tasks(vexe, module_dir, '-prod run', '.out', module_tests, true) - tasks << new_tasks(vexe, run_dir, 'run', '.run.out', run_tests, false) - tasks << new_tasks(vexe, parser_dir, '-prod', '.out', parser_tests, false) + tasks.add(vexe, parser_dir, '-prod', '.out', parser_tests, false) + tasks.add(vexe, checker_dir, '-prod', '.out', checker_tests, false) + tasks.add(vexe, checker_dir, '-d mysymbol run', '.mysymbol.run.out', ['custom_comptime_define_error.vv'], false) + tasks.add(vexe, checker_dir, '-d mydebug run', '.mydebug.run.out', ['custom_comptime_define_if_flag.vv'], false) + tasks.add(vexe, checker_dir, '-d nodebug run', '.nodebug.run.out', ['custom_comptime_define_if_flag.vv'], false) + tasks.add(vexe, checker_dir, '--enable-globals run', '.run.out', ['globals_error.vv'], false) + tasks.add(vexe, global_dir, '--enable-globals', '.out', global_tests, false) + tasks.add(vexe, module_dir, '-prod run', '.out', module_tests, true) + tasks.add(vexe, run_dir, 'run', '.run.out', run_tests, false) tasks.run() } -fn new_tasks(vexe string, dir string, voptions string, result_extension string, tests []string, is_module bool) []TaskDescription { +fn (mut tasks []TaskDescription) add(vexe string, dir string, voptions string, result_extension string, tests []string, is_module bool) { paths := vtest.filter_vtest_only(tests, { basepath: dir }) - mut res := []TaskDescription{} for path in paths { - res << TaskDescription{ + tasks << TaskDescription{ vexe: vexe dir: dir voptions: voptions @@ -71,7 +73,11 @@ fn new_tasks(vexe string, dir string, voptions string, result_extension string, is_module: is_module } } - return res +} + + +fn bstep_message(mut bench benchmark.Benchmark, label string, msg string, sduration time.Duration) string { + return bench.step_message_with_label_and_duration(label, msg, sduration) } // process an array of tasks in parallel, using no more than vjobs worker threads @@ -109,15 +115,13 @@ fn (mut tasks []TaskDescription) run() { bench.step() if task.is_skipped { bench.skip() - eprintln(bench.step_message_with_label_and_duration(benchmark.b_skip, task.path, - task.took)) + eprintln(bstep_message(mut bench, benchmark.b_skip, task.path, task.took)) continue } if task.is_error { total_errors++ bench.fail() - eprintln(bench.step_message_with_label_and_duration(benchmark.b_fail, task.path, - task.took)) + eprintln(bstep_message(mut bench, benchmark.b_fail, task.path, task.took)) println('============') println('expected:') println(task.expected) @@ -128,8 +132,7 @@ fn (mut tasks []TaskDescription) run() { diff_content(task.expected, task.found___) } else { bench.ok() - eprintln(bench.step_message_with_label_and_duration(benchmark.b_ok, task.path, - task.took)) + eprintln(bstep_message(mut bench, benchmark.b_ok, task.path, task.took)) } } bench.stop()