checker: add more checks for hash_stmt (#6615)

pull/6621/head
Swastik Baranwal 2020-10-15 14:28:01 +05:30 committed by GitHub
parent 60296c8900
commit 314fae7446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 11 deletions

View File

@ -629,6 +629,7 @@ pub:
pos token.Position
pub mut:
val string
kind string
}
/*

View File

@ -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 `<header_file.h>` 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)
}
}
}

View File

@ -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')
}
}

View File

@ -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
}
}