checker: minor changes to `Checker.hash_stmt()` (#9249)

pull/9257/head
StunxFS 2021-03-11 16:43:42 -04:00 committed by GitHub
parent 8f08795689
commit 04c4f16f75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 64 additions and 59 deletions

View File

@ -3486,87 +3486,92 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) {
} }
if c.pref.backend == .js { if c.pref.backend == .js {
if !c.file.path.ends_with('.js.v') { if !c.file.path.ends_with('.js.v') {
c.error('Hash statements are only allowed in backend specific files such "x.js.v"', c.error('hash statements are only allowed in backend specific files such "x.js.v"',
node.pos) node.pos)
} }
if c.mod == 'main' { if c.mod == 'main' {
c.error('Hash statements are not allowed in the main module. Please place them in a separate module.', c.error('hash statements are not allowed in the main module. Please place them in a separate module.',
node.pos) node.pos)
} }
return return
} }
if node.kind == 'include' { match node.kind {
mut flag := node.main 'include' {
if flag.contains('@VROOT') { mut flag := node.main
vroot := util.resolve_vroot(flag, c.file.path) or { if flag.contains('@VROOT') {
vroot := util.resolve_vroot(flag, c.file.path) or {
c.error(err.msg, node.pos)
return
}
node.val = 'include $vroot'
node.main = vroot
flag = vroot
}
if flag.contains('\$env(') {
env := util.resolve_env_value(flag, true) or {
c.error(err.msg, node.pos)
return
}
node.main = env
}
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 `<header_file.h>` quoting',
node.pos)
}
}
'pkgconfig' {
args := if node.main.contains('--') {
node.main.split(' ')
} else {
'--cflags --libs $node.main'.split(' ')
}
mut m := pkgconfig.main(args) or {
c.error(err.msg, node.pos) c.error(err.msg, node.pos)
return return
} }
node.val = 'include $vroot' cflags := m.run() or {
node.main = vroot
flag = vroot
}
if flag.contains('\$env(') {
env := util.resolve_env_value(flag, true) or {
c.error(err.msg, node.pos) c.error(err.msg, node.pos)
return return
} }
node.main = env c.table.parse_cflag(cflags, c.mod, c.pref.compile_defines_all) or {
}
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 `<header_file.h>` quoting',
node.pos)
}
} else if node.kind == 'pkgconfig' {
args := if node.main.contains('--') {
node.main.split(' ')
} else {
'--cflags --libs $node.main'.split(' ')
}
mut m := pkgconfig.main(args) or {
c.error(err.msg, node.pos)
return
}
cflags := m.run() or {
c.error(err.msg, node.pos)
return
}
c.table.parse_cflag(cflags, c.mod, c.pref.compile_defines_all) or {
c.error(err.msg, node.pos)
return
}
} else if node.kind == 'flag' {
// #flag linux -lm
mut flag := node.main
// expand `@VROOT` to its absolute path
if flag.contains('@VROOT') {
flag = util.resolve_vroot(flag, c.file.path) or {
c.error(err.msg, node.pos) c.error(err.msg, node.pos)
return return
} }
} }
if flag.contains('\$env(') { 'flag' {
flag = util.resolve_env_value(flag, true) or { // #flag linux -lm
mut flag := node.main
// expand `@VROOT` to its absolute path
if flag.contains('@VROOT') {
flag = util.resolve_vroot(flag, c.file.path) or {
c.error(err.msg, node.pos)
return
}
}
if flag.contains('\$env(') {
flag = util.resolve_env_value(flag, true) or {
c.error(err.msg, node.pos)
return
}
}
for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] {
if flag.contains(deprecated) {
c.error('$deprecated had been deprecated, use @VROOT instead.', node.pos)
}
}
// println('adding flag "$flag"')
c.table.parse_cflag(flag, c.mod, c.pref.compile_defines_all) or {
c.error(err.msg, node.pos) c.error(err.msg, node.pos)
return
} }
} }
for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { else {
if flag.contains(deprecated) { if node.kind != 'define' {
c.error('$deprecated had been deprecated, use @VROOT instead.', node.pos) c.error('expected `#define`, `#flag`, `#include` or `#pkgconfig` not $node.val',
node.pos)
} }
} }
// println('adding flag "$flag"')
c.table.parse_cflag(flag, c.mod, c.pref.compile_defines_all) or {
c.error(err.msg, node.pos)
}
} else {
if node.kind != 'define' {
c.error('expected `#define`, `#flag`, `#include` or `#pkgconfig` not $node.val',
node.pos)
}
} }
} }