From 314fae7446272ba0dbb86a4f3bd570c82bf2838a Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Thu, 15 Oct 2020 14:28:01 +0530 Subject: [PATCH] checker: add more checks for hash_stmt (#6615) --- vlib/v/ast/ast.v | 1 + vlib/v/checker/checker.v | 16 ++++++++++++++-- vlib/v/gen/cgen.v | 8 ++++---- vlib/v/parser/comptime.v | 14 +++++++++----- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index d8983d5f8a..9b9da0be58 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -629,6 +629,7 @@ pub: pos token.Position pub mut: val string + kind string } /* diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8fc31ee49a..4447d3948a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2462,7 +2462,9 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { c.error('Hash statements are not allowed in the main module. Please place them in a separate module.', node.pos) } - } else if node.val.starts_with('include') { + return + } + if node.kind == 'include' { mut flag := node.val[8..] if flag.contains('@VROOT') { vroot := util.resolve_vroot(flag, c.file.path) or { @@ -2471,7 +2473,13 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { } node.val = 'include $vroot' } - } else if node.val.starts_with('flag') { + flag_no_comment := flag.all_before('//').trim_space() + if !((flag_no_comment.starts_with('"') && flag_no_comment.ends_with('"')) || + (flag_no_comment.starts_with('<') && flag_no_comment.ends_with('>'))) { + c.error('including C files should use either `"header_file.h"` or `` quoting', + node.pos) + } + } else if node.kind == 'flag' { // #flag linux -lm mut flag := node.val[5..] // expand `@VROOT` to its absolute path @@ -2490,6 +2498,10 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { c.table.parse_cflag(flag, c.mod, c.pref.compile_defines_all) or { c.error(err, node.pos) } + } else { + if node.kind != 'define' { + c.warn('expected `#include`, `#flag` or `#define` not $node.val', node.pos) + } } } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 127e55811b..b29a21b548 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -959,18 +959,18 @@ fn (mut g Gen) stmt(node ast.Stmt) { } ast.HashStmt { // #include etc - typ := node.val.all_before(' ') - if typ == 'include' { + if node.kind == 'include' { if node.val.contains('.m') { // Objective C code import, include it after V types, so that e.g. `string` is // available there + g.definitions.writeln('// added by module `$node.mod`:') g.definitions.writeln('#$node.val') } else { g.includes.writeln('// added by module `$node.mod`:') g.includes.writeln('#$node.val') } - } - if typ == 'define' { + } else if node.kind == 'define' { + g.includes.writeln('// defined by module `$node.mod`:') g.includes.writeln('#$node.val') } } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 1173b98c06..eff9ddd792 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -11,19 +11,23 @@ import vweb.tmpl // #flag darwin -I. const ( - supported_platforms = ['windows', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', - 'netbsd', 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] + supported_platforms = ['windows', 'macos', 'darwin', 'linux', 'freebsd', 'openbsd', 'netbsd', + 'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos'] supported_ccompilers = ['tinyc', 'clang', 'mingw', 'msvc', 'gcc'] ) // // #include, #flag, #v fn (mut p Parser) hash() ast.HashStmt { - mut val := p.tok.lit + mut pos := p.prev_tok.position() + val := p.tok.lit + kind := val.all_before(' ') p.next() + //p.trace('a.v', 'kind: ${kind:-10s} | pos: ${pos:-45s} | hash: $val') return ast.HashStmt{ - val: val mod: p.mod - pos: p.prev_tok.position() + val: val + kind: kind + pos: pos } }