Сценарий: вызов memchecker.DeletePointer() меняется на delete this;
Этот коммит содержится в:
родитель
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 {
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче