Сценарий: вызов memchecker.DeletePointer() меняется на delete this;

Этот коммит содержится в:
Softonik 2024-02-14 04:48:43 +03:00 коммит произвёл Nobody
родитель bbb46751ee
коммит 9b117e1676
6 изменённых файлов: 79 добавлений и 27 удалений

Просмотреть файл

@ -50,7 +50,7 @@ func (c *class) classDefinitionToString() (code string) {
code += ": public " + c.baseInterface
}
code += " {"
code += " {\n"
code += "public:\n"
code += c.structToString()

Просмотреть файл

@ -27,7 +27,7 @@ func (c *abstractClass) MethodsString() (code string) {
}
func (c *abstractClass) abstractClassDefinitionToString() (code string) {
code = "class " + c.name + " {"
code = "class " + c.name + " {\n"
code += "public:\n"
code += c.methodDeclarationsToString()

Просмотреть файл

@ -3,7 +3,6 @@ package service
import (
"go/ast"
"go/token"
"strings"
)
func handleExpr(expr ast.Expr) (code string) {
@ -77,20 +76,7 @@ func handleBinaryExpr(expr ast.Expr) string {
return code
}
func handleCallExpr(expr *ast.CallExpr) (code string) {
code = handleExpr(expr.Fun)
code += "("
args := make([]string, 0)
for _, arg := range expr.Args {
args = append(args, handleExpr(arg))
}
code += strings.Join(args, ",")
code += ")"
return
}
func handleIdentExpr(ident *ast.Ident) string {
code := ""
func handleIdentExpr(ident *ast.Ident) (code string) {
switch ident.Name {
case "nil":
code += "NULL"
@ -105,13 +91,12 @@ func handleIdentExpr(ident *ast.Ident) string {
default:
code += ident.Name
}
return code
return
}
func handleParenExpr(stmt *ast.ParenExpr) string {
code := ""
func handleParenExpr(stmt *ast.ParenExpr) (code string) {
code += handleExpr(stmt.X)
return code
return
}
func handleSelectorExpr(s *ast.SelectorExpr) (code string) {

Просмотреть файл

@ -182,6 +182,29 @@ public:
};
void device::doSomething(int* a) {
}
```
Сценарий: Структура с деструктором Close: вызов memchecker.DeletePointer() меняется на delete this;
* Исходник:
```
package test
type device struct {
}
func (d *device) Close() {
memchecker.DeletePointer(d)
}
```
* Результат:
```
class device {
public:
void Close();
};
void device::Close() {
delete this;
}
```
Сценарий: Структура с вызовом метода

Просмотреть файл

@ -157,3 +157,46 @@ func addFunctionDeclaration(f string) {
funcDeclarations = append(funcDeclarations, f)
}
func handleCallExpr(expr *ast.CallExpr) (code string) {
// println()
// spew.Dump(expr)
if isMemcheckerDeletePointerCall(expr.Fun) {
return handleCallAsDelete(expr)
}
return handleCallExprNormal(expr)
}
func isMemcheckerDeletePointerCall(expr ast.Expr) bool {
// println()
// spew.Dump(expr)
s, ok := expr.(*ast.SelectorExpr)
if !ok {
return false
}
if handleExpr(s.X) != "memchecker" {
return false
}
if handleIdentExpr(s.Sel) != "DeletePointer" {
return false
}
return true
}
func handleCallAsDelete(expr *ast.CallExpr) (code string) {
code += "delete this"
return
}
func handleCallExprNormal(expr *ast.CallExpr) (code string) {
code += handleExpr(expr.Fun)
code += "("
args := make([]string, 0)
for _, arg := range expr.Args {
args = append(args, handleExpr(arg))
}
code += strings.Join(args, ",")
code += ")"
return
}

Просмотреть файл

@ -166,17 +166,18 @@ func addGoHelperDeclaration(f string) {
helpersForDeclarations = append(helpersForDeclarations, f)
}
func handleExprStmt(stmt *ast.ExprStmt) string {
code := ""
func handleExprStmt(stmt *ast.ExprStmt) (code string) {
// println()
// spew.Dump(stmt)
switch x := stmt.X.(type) {
case *ast.CallExpr:
code += handleCallExpr(x)
}
return code
return
}
func handleForStmt(stmt *ast.ForStmt) string {
code := ""
func handleForStmt(stmt *ast.ForStmt) (code string) {
is_while := false
if stmt.Init == nil && stmt.Post == nil {
is_while = true
@ -203,7 +204,7 @@ func handleForStmt(stmt *ast.ForStmt) string {
code += ") {\n"
code += handleBlockStmt(stmt.Body)
code += "}"
return code
return
}
func handleIfStmt(stmt *ast.IfStmt) string {