nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: Konstantin8105/c4go
Pull request #262 (2019-02-09 13:09:00)

transpileEnumDeclWithType: fix bug for char enum


Patch:
diff --git a/transpiler/enum.go b/transpiler/enum.go
index 9e1c0b1..534a104 100644
--- a/transpiler/enum.go
+++ b/transpiler/enum.go
@@ -92,7 +92,7 @@ func transpileEnumDeclWithType(p *program.Program, n *ast.EnumDecl, enumType str
 	decls []goast.Decl, err error) {
 	defer func() {
 		if err != nil {
-			err = fmt.Errorf("Cannot transpileEnumDeclWithoutName. %v", err)
+			err = fmt.Errorf("Cannot transpileEnumDeclWithName. %v", err)
 		}
 	}()
 	preStmts := []goast.Stmt{}
@@ -163,15 +163,9 @@ func transpileEnumDeclWithType(p *program.Program, n *ast.EnumDecl, enumType str
 		case *goast.BasicLit:
 			var value int
 			value, err = strconv.Atoi(v.Value)
-			if err != nil {
-				err = fmt.Errorf("Cannot parse '%s' in BasicLit", v.Value)
-				return
-			}
 			if err != nil {
 				e = val
 				counter++
-				p.AddMessage(p.GenerateWarningMessage(
-					fmt.Errorf("Cannot parse '%s' in BasicLit", v.Value), n))
 				break
 			}
 			if sign == -1 {
diff --git a/transpiler/unary.go b/transpiler/unary.go
index 3e8b8f3..133c924 100644
--- a/transpiler/unary.go
+++ b/transpiler/unary.go
@@ -600,6 +600,8 @@ func transpileUnaryExprOrTypeTraitExpr(n *ast.UnaryExprOrTypeTraitExpr, p *progr
 			realFirstChild = c.Children()[0]
 		case *ast.DeclRefExpr:
 			t = c.Type
+		case *ast.UnaryOperator:
+			t = c.Type
 		case *ast.MemberExpr:
 			t = c.Type
 		case *ast.ArraySubscriptExpr:


Test patch:
diff --git a/tests/enum.c b/tests/enum.c
index d9a60fb..1027056 100644
--- a/tests/enum.c
+++ b/tests/enum.c
@@ -137,10 +137,21 @@ typedef enum {
 enum { e1 = 1,
     e2 };
 
+enum {
+	MBchar = 'U', 
+	Troffchar = 'C', 
+	Number = 'N', 
+	Install = 'i', 
+	Lookup = 'l' 
+};
+
+
 // main function
 int main()
 {
-    plan(43);
+    plan(44);
+
+	is_eq(MBchar, 'U');
 
     test_unary();
     test_parent();
diff --git a/tests/unary.c b/tests/unary.c
index c265081..74e6862 100644
--- a/tests/unary.c
+++ b/tests/unary.c
@@ -39,12 +39,35 @@ void test_notptr()
     }
 }
 
+int conv(int i)
+{
+	return i;
+}
+
+void test_sizeoftest()
+{
+	// example :
+	// while ((n = fread(buf, sizeof *buf, sizeof buf, fin)) > 0)
+// UnaryExprOrTypeTraitExpr 'unsigned long' sizeof
+// `-UnaryOperator 0x2e1a880 'char' lvalue prefix '*'
+//   `-ImplicitCastExpr 0x2e1a868 'char *' <ArrayToPointerDecay>
+//     `-DeclRefExpr 0x2e1a840 'char [1024]' lvalue Var 0x2e19d78 'buf' 'char [1024]'
+	char *buf = NULL;
+	if (conv(sizeof *buf) > 0) {
+		pass("ok");
+	} else {
+		fail("fail");
+	}
+	(void)(buf);
+}
+
 int main()
 {
-    plan(4);
+    plan(5);
 
     START_TEST(notint)
     START_TEST(notptr)
+	START_TEST(sizeoftest)
 
     done_testing();
 }
Repository: Konstantin8105/c4go
Pull request #268 (2019-02-09 19:42:01)

transpileCall: operator = or += in arguments


Patch:
diff --git a/transpiler/call.go b/transpiler/call.go
index 87c5683..4c9c3c8 100644
--- a/transpiler/call.go
+++ b/transpiler/call.go
@@ -5,6 +5,10 @@ package transpiler
 import (
 	"bytes"
 	"fmt"
+	goast "go/ast"
+	"go/parser"
+	"go/printer"
+	"go/token"
 	"strconv"
 	"strings"
 
@@ -12,11 +16,6 @@ import (
 	"github.com/Konstantin8105/c4go/program"
 	"github.com/Konstantin8105/c4go/types"
 	"github.com/Konstantin8105/c4go/util"
-
-	goast "go/ast"
-	"go/parser"
-	"go/printer"
-	"go/token"
 )
 
 func getMemberName(firstChild ast.Node) (name string, ok bool) {
@@ -409,8 +408,31 @@ func transpileCallExpr(n *ast.CallExpr, p *program.Program) (
 	args := []goast.Expr{}
 	argTypes := []string{}
 	i := 0
-
 	for _, arg := range n.Children()[1:] {
+		if bin, ok := arg.(*ast.BinaryOperator); ok && bin.Operator == "=" {
+			// example :
+			// from :
+			// call(val = 43);
+			// to:
+			// call(val = 43,val);
+			var b ast.BinaryOperator
+			b.Operator = ","
+			b.AddChild(arg)
+			b.AddChild(bin.Children()[0])
+			arg = &b
+		}
+		if cmp, ok := arg.(*ast.CompoundAssignOperator); ok {
+			// example :
+			// from :
+			// call(val += 43);
+			// to:
+			// call(val += 43,val);
+			var b ast.BinaryOperator
+			b.Operator = ","
+			b.AddChild(arg)
+			b.AddChild(cmp.Children()[0])
+			arg = &b
+		}
 		e, eType, newPre, newPost, err := transpileToExpr(arg, p, false)
 		if err != nil {
 			err = fmt.Errorf("argument position is %d. %v", i, err)


Test patch:
diff --git a/tests/function.c b/tests/function.c
index 968158e..694dd46 100644
--- a/tests/function.c
+++ b/tests/function.c
@@ -216,7 +216,7 @@ void NullPointerCheck()
 
 int main()
 {
-    plan(63);
+    plan(65);
 
     test_string();
     test_null_function();
@@ -352,6 +352,15 @@ int main()
 	test_function_if();
 	NullPointerCheck();
 
+	{
+		diag("not argument in argument");
+		int t = 0;
+		exit2(t = 2);
+		is_eq(t,2);
+		exit2(t += 2);
+		is_eq(t,4);
+	}
+
     done_testing();
 }
Repository: Konstantin8105/c4go
Pull request #269 (2019-02-09 20:20:07)

atomicOperation: for transpiling If


Patch:
diff --git a/transpiler/branch.go b/transpiler/branch.go
index 90b93aa..dd22b97 100644
--- a/transpiler/branch.go
+++ b/transpiler/branch.go
@@ -67,16 +67,7 @@ func transpileIfStmt(n *ast.IfStmt, p *program.Program) (
 
 	// The last parameter must be false because we are transpiling an
 	// expression - assignment operators need to be wrapped in closures.
-	if cao, ok := children[1].(*ast.CompoundAssignOperator); ok {
-		// example:
-		// if ( val += step) {...}
-		var bin ast.BinaryOperator
-		bin.Operator = ","
-		bin.AddChild(children[1])
-		bin.AddChild(cao.Children()[1])
-		children[1] = &bin
-	}
-	conditional, conditionalType, newPre, newPost, err := transpileToExpr(children[1], p, false)
+	conditional, conditionalType, newPre, newPost, err := atomicOperation(children[1], p)
 	if err != nil {
 		err = fmt.Errorf("Cannot transpile for condition. %v", err)
 		return nil, nil, nil, err
diff --git a/transpiler/operators.go b/transpiler/operators.go
index 2fd7ef2..e8adbba 100644
--- a/transpiler/operators.go
+++ b/transpiler/operators.go
@@ -1034,6 +1034,8 @@ func getDeclRefExprOrArraySub(n ast.Node) (ast.Node, bool) {
 		return getDeclRefExprOrArraySub(n.Children()[0])
 	case *ast.ArraySubscriptExpr:
 		return v, true
+	case *ast.MemberExpr:
+		return v, true
 	}
 	return nil, false
 }


Test patch:
diff --git a/tests/if.c b/tests/if.c
index 2b32f7c..36dae25 100644
--- a/tests/if.c
+++ b/tests/if.c
@@ -8,7 +8,7 @@ int d(int v)
 
 int main()
 {
-    plan(23);
+    plan(24);
 
     int x = 1;
 
@@ -168,5 +168,13 @@ int main()
         fail("y");
 	}
 
+	struct yS {int *c;};
+	struct yS y2;
+	if ((y2.c = (int*)calloc(1,sizeof(char))) != NULL){
+		pass("y2");
+	} else {
+        fail("y2");
+	}
+
     done_testing();
 }
Repository: Konstantin8105/c4go
Pull request #270 (2019-02-14 08:28:41)

transpileReturn: add atomic


Patch:
diff --git a/transpiler/functions.go b/transpiler/functions.go
index 0801ad8..cd8cf26 100644
--- a/transpiler/functions.go
+++ b/transpiler/functions.go
@@ -326,7 +326,7 @@ func transpileReturnStmt(n *ast.ReturnStmt, p *program.Program) (
 
 	var eType string
 	var e goast.Expr
-	e, eType, preStmts, postStmts, err = transpileToExpr(n.Children()[0], p, false)
+	e, eType, preStmts, postStmts, err = atomicOperation(n.Children()[0], p)
 	if err != nil {
 		return nil, nil, nil, err
 	}


Test patch:
diff --git a/tests/operators.c b/tests/operators.c
index f99ebca..aa0210a 100644
--- a/tests/operators.c
+++ b/tests/operators.c
@@ -45,9 +45,21 @@ int f_sizeof(int i)
 	return i;
 }
 
+int reteg(int a)
+{
+	int arr[5];
+	for (int i = 0; i < 5;i++) {
+			arr[i] = i;
+	}
+	int *ptr;
+	ptr = &arr[1];
+	(void)(ptr);
+	return *ptr = a+1;
+}
+
 int main()
 {
-    plan(110);
+    plan(111);
 
     int i = 10;
     signed char j = 1;
@@ -451,6 +463,12 @@ int main()
 		is_true(y[1] == 5);
 		is_true(y[sizeof(char)] == 5);
 	}
+	diag("function with equal in return");
+	{
+		int a = 42;
+		a = reteg(a);
+		is_eq(a,43);
+	}
 
     done_testing();
 }
Repository: Konstantin8105/c4go
Pull request #272 (2019-02-14 11:52:57)

ast: add UsedAttr


Patch:
diff --git a/ast/ast.go b/ast/ast.go
index e00403a..b64d67f 100644
--- a/ast/ast.go
+++ b/ast/ast.go
@@ -273,6 +273,8 @@ func Parse(fullline string) (returnNode Node, err error) {
 		return parseUnaryOperator(line), nil
 	case "UnusedAttr":
 		return parseUnusedAttr(line), nil
+	case "UsedAttr":
+		return parseUsedAttr(line), nil
 	case "VAArgExpr":
 		return parseVAArgExpr(line), nil
 	case "VarDecl":
diff --git a/ast/position.go b/ast/position.go
index a34ae63..13de67b 100644
--- a/ast/position.go
+++ b/ast/position.go
@@ -408,6 +408,8 @@ func setPosition(node Node, position Position) {
 		n.Pos = position
 	case *UnusedAttr:
 		n.Pos = position
+	case *UsedAttr:
+		n.Pos = position
 	case *VAArgExpr:
 		n.Pos = position
 	case *VarDecl:
diff --git a/ast/used_attr.go b/ast/used_attr.go
new file mode 100644
index 0000000..fb622c1
--- /dev/null
+++ b/ast/used_attr.go
@@ -0,0 +1,44 @@
+package ast
+
+// UsedAttr is attribute
+type UsedAttr struct {
+	Addr       Address
+	Pos        Position
+	ChildNodes []Node
+}
+
+func parseUsedAttr(line string) *UsedAttr {
+	groups := groupsFromRegex(
+		"<(?P<position>.*)>",
+		line,
+	)
+
+	return &UsedAttr{
+		Addr:       ParseAddress(groups["address"]),
+		Pos:        NewPositionFromString(groups["position"]),
+		ChildNodes: []Node{},
+	}
+}
+
+// AddChild adds a new child node. Child nodes can then be accessed with the
+// Children attribute.
+func (n *UsedAttr) AddChild(node Node) {
+	n.ChildNodes = append(n.ChildNodes, node)
+}
+
+// Address returns the numeric address of the node. See the documentation for
+// the Address type for more information.
+func (n *UsedAttr) Address() Address {
+	return n.Addr
+}
+
+// Children returns the child nodes. If this node does not have any children or
+// this node does not support children it will always return an empty slice.
+func (n *UsedAttr) Children() []Node {
+	return n.ChildNodes
+}
+
+// Position returns the position in the original source code.
+func (n *UsedAttr) Position() Position {
+	return n.Pos
+}


Test patch:
diff --git a/ast/used_attr_test.go b/ast/used_attr_test.go
new file mode 100644
index 0000000..c3c4d4c
--- /dev/null
+++ b/ast/used_attr_test.go
@@ -0,0 +1,17 @@
+package ast
+
+import (
+	"testing"
+)
+
+func TestUsedAttr(t *testing.T) {
+	nodes := map[string]Node{
+		`0x3be4e70 <col:44>`: &UsedAttr{
+			Addr:       0x3be4e70,
+			Pos:        NewPositionFromString("col:44"),
+			ChildNodes: []Node{},
+		},
+	}
+
+	runNodeTests(t, nodes)
+}
Repository: Konstantin8105/c4go
Pull request #283 (2019-02-25 13:56:38)

cstd: errno.h


Patch:
diff --git a/noarch/errno.go b/noarch/errno.go
new file mode 100644
index 0000000..66decfc
--- /dev/null
+++ b/noarch/errno.go
@@ -0,0 +1,7 @@
+package noarch
+
+var errno int
+
+func ErrnoLocation() []int {
+	return []int{errno}
+}
diff --git a/program/definition_function.go b/program/definition_function.go
index 15198d1..b461f88 100644
--- a/program/definition_function.go
+++ b/program/definition_function.go
@@ -58,6 +58,9 @@ type DefinitionFunction struct {
 //     size_t fread(void*, size_t, size_t, FILE*) -> $0 = noarch.Fread(&1, $2, $3, $4)
 //
 var builtInFunctionDefinitions = map[string][]string{
+	"errno.h": {
+		"int * __errno_location(void ) -> noarch.ErrnoLocation",
+	},
 	"assert.h": {
 		// linux/assert.h
 		"bool __assert_fail(const char*, const char*, unsigned int, const char*) -> linux.AssertFail",


Test patch:
diff --git a/tests/errno.c b/tests/errno.c
new file mode 100644
index 0000000..d8faa88
--- /dev/null
+++ b/tests/errno.c
@@ -0,0 +1,18 @@
+#include "tests.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
+
+int main()
+{
+    plan(1);
+
+	if (__errno_location() == NULL) {
+		fail("not ok");
+	} else {
+		pass("ok");
+	}
+
+    done_testing();
+}