cgen: fix use of C reserved words inside `defer` statement (fix #14101) (#14211)

master
StunxFS 2022-04-29 01:04:59 -04:00 committed by GitHub
parent 77645fcf35
commit 8b798acadd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 12 deletions

View File

@ -299,7 +299,7 @@ fn (mut g Gen) gen_assign_stmt(node_ ast.AssignStmt) {
g.write(' = ${styp}_${util.replace_op(extracted_op)}(') g.write(' = ${styp}_${util.replace_op(extracted_op)}(')
method := g.table.find_method(left_sym, extracted_op) or { method := g.table.find_method(left_sym, extracted_op) or {
// the checker will most likely have found this, already... // the checker will most likely have found this, already...
g.error('assignemnt operator `$extracted_op=` used but no `$extracted_op` method defined', g.error('assignment operator `$extracted_op=` used but no `$extracted_op` method defined',
node.pos) node.pos)
ast.Fn{} ast.Fn{}
} }

View File

@ -15,9 +15,10 @@ import v.depgraph
import sync.pool import sync.pool
const ( const (
// Note: some of the words in c_reserved, are not reserved in C, // Note: some of the words in c_reserved, are not reserved in C, but are
// but are in C++, or have special meaning in V, thus need escaping too. // in C++, or have special meaning in V, thus need escaping too. `small`
// `small` should not be needed, but see: https://stackoverflow.com/questions/5874215/what-is-rpcndr-h // should not be needed, but see:
// https://stackoverflow.com/questions/5874215/what-is-rpcndr-h
c_reserved = ['array', 'auto', 'bool', 'break', 'calloc', 'case', 'char', 'class', 'complex', c_reserved = ['array', 'auto', 'bool', 'break', 'calloc', 'case', 'char', 'class', 'complex',
'const', 'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'error', 'exit', 'const', 'continue', 'default', 'delete', 'do', 'double', 'else', 'enum', 'error', 'exit',
'export', 'extern', 'false', 'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link', 'export', 'extern', 'false', 'float', 'for', 'free', 'goto', 'if', 'inline', 'int', 'link',
@ -1590,10 +1591,6 @@ fn (mut g Gen) stmt(node ast.Stmt) {
if !g.skip_stmt_pos { if !g.skip_stmt_pos {
g.set_current_pos_as_last_stmt_pos() g.set_current_pos_as_last_stmt_pos()
} }
defer {
}
// println('g.stmt()')
// g.writeln('//// stmt start')
match node { match node {
ast.EmptyStmt {} ast.EmptyStmt {}
ast.AsmStmt { ast.AsmStmt {

View File

@ -337,7 +337,7 @@ fn (mut g Gen) gen_fn_decl(node &ast.FnDecl, skip bool) {
} }
info := var.obj as ast.Var info := var.obj as ast.Var
if g.table.sym(info.typ).kind != .function { if g.table.sym(info.typ).kind != .function {
g.writeln('${g.typ(info.typ)}$deref $var.name;') g.writeln('${g.typ(info.typ)}$deref ${c_name(var.name)};')
} }
} }
} }

View File

@ -154,3 +154,24 @@ fn num() int {
} }
return ret return ret
} }
fn close(i int) {
eprintln('Close $i')
}
fn test_defer_with_reserved_words() {
if 1 == 1 {
single := 1
defer {
close(single)
}
}
if 2 == 2 {
double := 9
defer {
close(double)
}
}
eprintln('Done')
assert true
}