From f9fe5b60a72f0d22bceadd356e742672e2ddfedb Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 5 May 2020 08:00:52 +0300 Subject: [PATCH] v doc: skip _x.c.v when not on x, i.e. filter files like v itself --- cmd/v/v.v | 2 +- vlib/v/builder/builder.v | 75 ++---------------------------------- vlib/v/doc/doc.v | 21 ++++------ vlib/v/doc/doc_test.v | 5 ++- vlib/v/pref/should_compile.v | 74 +++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 87 deletions(-) create mode 100644 vlib/v/pref/should_compile.v diff --git a/cmd/v/v.v b/cmd/v/v.v index 386c7f19c8..f7dc229dcd 100644 --- a/cmd/v/v.v +++ b/cmd/v/v.v @@ -93,7 +93,7 @@ fn main() { exit(1) } table := table.new_table() - println(doc.doc(args[1], table)) + println(doc.doc(args[1], table, prefs)) return } else {} diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index c64de9da9d..f7c1d3645c 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -87,7 +87,7 @@ pub fn (mut b Builder) parse_imports() { for p in b.parsed_files { println(p.path) } - exit(0) + exit(0) } } @@ -144,7 +144,6 @@ pub fn (b &Builder) import_graph() &depgraph.DepGraph { } pub fn (b Builder) v_files_from_dir(dir string) []string { - mut res := []string{} if !os.exists(dir) { if dir == 'compiler' && os.is_dir('vlib') { println('looks like you are trying to build V with an old command') @@ -160,75 +159,7 @@ pub fn (b Builder) v_files_from_dir(dir string) []string { if b.pref.is_verbose { println('v_files_from_dir ("$dir")') } - files.sort() - for file in files { - if !file.ends_with('.v') && !file.ends_with('.vh') { - continue - } - if file.ends_with('_test.v') { - continue - } - if b.pref.backend == .c && !b.should_compile_c(file) { - continue - } - if b.pref.backend == .js && !b.should_compile_js(file) { - continue - } - if b.pref.compile_defines_all.len > 0 && file.contains('_d_') { - mut allowed := false - for cdefine in b.pref.compile_defines { - file_postfix := '_d_${cdefine}.v' - if file.ends_with(file_postfix) { - allowed = true - break - } - } - if !allowed { - continue - } - } - res << os.join_path(dir, file) - } - return res -} - -[inline] -fn (b Builder) should_compile_c(file string) bool { - if !file.ends_with('.c.v') && file.split('.').len > 2 { - // Probably something like `a.js.v`. - return false - } - if file.ends_with('_windows.c.v') && b.pref.os != .windows { - return false - } - if file.ends_with('_linux.c.v') && b.pref.os != .linux { - return false - } - if file.ends_with('_darwin.c.v') && b.pref.os != .mac { - return false - } - if file.ends_with('_nix.c.v') && b.pref.os == .windows { - return false - } - if file.ends_with('_android.c.v') && b.pref.os != .android { - return false - } - if file.ends_with('_freebsd.c.v') && b.pref.os != .freebsd { - return false - } - if file.ends_with('_solaris.c.v') && b.pref.os != .solaris { - return false - } - return true -} - -[inline] -fn (b Builder) should_compile_js(file string) bool { - if !file.ends_with('.js.v') && file.split('.').len > 2 { - // Probably something like `a.c.v`. - return false - } - return true + return b.pref.should_compile_filtered_files(dir, files) } pub fn (b Builder) log(s string) { @@ -274,7 +205,7 @@ pub fn (b Builder) find_module_path(mod, fpath string) ?string { return error('module "$mod" not found in:\n$smodule_lookup_paths') } -fn (b &Builder) print_warnings_and_errors(){ +fn (b &Builder) print_warnings_and_errors() { if b.checker.nr_warnings > 0 { for err in b.checker.warnings { kind := if b.pref.is_verbose { '$err.reporter warning #$b.checker.nr_warnings:' } else { 'warning:' } diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 72e76d5b85..24cbb44e0b 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -17,7 +17,7 @@ mut: type FilterFn = fn (node ast.FnDecl) bool -pub fn doc(mod string, table &table.Table) string { +pub fn doc(mod string, table &table.Table, prefs &pref.Preferences) string { mut d := Doc{ out: strings.new_builder(1000) table: table @@ -35,17 +35,12 @@ pub fn doc(mod string, table &table.Table) string { files := os.ls(path) or { panic(err) } - for file in files { - if !file.ends_with('.v') { - continue - } - if file.ends_with('_test.v') || file.ends_with('_windows.c.v') || file.ends_with('_macos.c.v') { - continue - } - file_ast := parser.parse_file(os.join_path(path, file), table, .skip_comments, &pref.Preferences{}, - &ast.Scope{ + filtered_files := prefs.should_compile_filtered_files(path, files) + for file in filtered_files { + fscope := &ast.Scope{ parent: 0 - }) + } + file_ast := parser.parse_file(file, table, .skip_comments, prefs, fscope) d.stmts << file_ast.stmts } if d.stmts.len == 0 { @@ -59,8 +54,8 @@ pub fn doc(mod string, table &table.Table) string { d.print_methods() /* for stmt in file_ast.stmts { - d.stmt(stmt) - } + d.stmt(stmt) + } println(path) */ return d.out.str().trim_space() diff --git a/vlib/v/doc/doc_test.v b/vlib/v/doc/doc_test.v index 5fe79cbe18..4a0b54e47d 100644 --- a/vlib/v/doc/doc_test.v +++ b/vlib/v/doc/doc_test.v @@ -1,7 +1,10 @@ import v.table import v.doc +import v.pref fn test_vdoc() { + mut prefs := &pref.Preferences{} + prefs.fill_with_defaults() table := table.new_table() - println(doc.doc('net', table)) + println(doc.doc('net', table, prefs)) } diff --git a/vlib/v/pref/should_compile.v b/vlib/v/pref/should_compile.v new file mode 100644 index 0000000000..3233d50621 --- /dev/null +++ b/vlib/v/pref/should_compile.v @@ -0,0 +1,74 @@ +module pref + +import os + +pub fn (prefs &Preferences) should_compile_filtered_files(dir string, files []string) []string { + mut res := []string{} + files.sort() + for file in files { + if !file.ends_with('.v') && !file.ends_with('.vh') { + continue + } + if file.ends_with('_test.v') { + continue + } + if prefs.backend == .c && !prefs.should_compile_c(file) { + continue + } + if prefs.backend == .js && !prefs.should_compile_js(file) { + continue + } + if prefs.compile_defines_all.len > 0 && file.contains('_d_') { + mut allowed := false + for cdefine in prefs.compile_defines { + file_postfix := '_d_${cdefine}.v' + if file.ends_with(file_postfix) { + allowed = true + break + } + } + if !allowed { + continue + } + } + res << os.join_path(dir, file) + } + return res +} + +pub fn (prefs &Preferences) should_compile_c(file string) bool { + if !file.ends_with('.c.v') && file.split('.').len > 2 { + // Probably something like `a.js.v`. + return false + } + if file.ends_with('_windows.c.v') && prefs.os != .windows { + return false + } + if file.ends_with('_linux.c.v') && prefs.os != .linux { + return false + } + if file.ends_with('_darwin.c.v') && prefs.os != .mac { + return false + } + if file.ends_with('_nix.c.v') && prefs.os == .windows { + return false + } + if file.ends_with('_android.c.v') && prefs.os != .android { + return false + } + if file.ends_with('_freebsd.c.v') && prefs.os != .freebsd { + return false + } + if file.ends_with('_solaris.c.v') && prefs.os != .solaris { + return false + } + return true +} + +pub fn (prefs &Preferences) should_compile_js(file string) bool { + if !file.ends_with('.js.v') && file.split('.').len > 2 { + // Probably something like `a.c.v`. + return false + } + return true +}