Repository: Konstantin8105/c4go
Pull request #288 (2019-02-27 11:32:45)

Sizeof


Patch:
diff --git a/program/definition_function.go b/program/definition_function.go
index 435b76f..8bb99e6 100644
--- a/program/definition_function.go
+++ b/program/definition_function.go
@@ -354,6 +354,13 @@ var builtInFunctionDefinitions = map[string][]string{
 		"ssize_t read(int, void *, size_t) -> noarch.Read",
 		"int close(int) -> golang.org/x/sys/unix.CloseOnExec",
 		"int isatty(int) -> noarch.Isatty",
+		"int ftruncate(int , off_t ) -> noarch.Ftruncate",
+		"int unlink(const char *) -> noarch.Unlink",
+	},
+	"sys/stat.h": {
+		"int fstat(int , struct stat  *) -> noarch.Fstat",
+		"int stat(const char * , struct stat * ) -> noarch.Stat",
+		"int lstat(const char * , struct stat * ) -> noarch.Lstat",
 	},
 }
 
diff --git a/program/definition_type.go b/program/definition_type.go
index f880048..28b3754 100644
--- a/program/definition_type.go
+++ b/program/definition_type.go
@@ -4,7 +4,8 @@ package program
 // c4go structures
 var DefinitionType = map[string]string{
 	// time.h
-	"time_t":        "github.com/Konstantin8105/c4go/noarch.TimeT",
+	"time_t": "github.com/Konstantin8105/c4go/noarch.TimeT",
+	"github.com/Konstantin8105/c4go/noarch.TimeT": "int32",
 	"__time_t":      "int32",
 	"__suseconds_t": "int32",
 
@@ -12,6 +13,7 @@ var DefinitionType = map[string]string{
 
 	// unistd.h
 	"ssize_t": "github.com/Konstantin8105/c4go/noarch.SsizeT",
+	"github.com/Konstantin8105/c4go/noarch.SsizeT": "long int",
 
 	// built-in
 	"bool":                   "bool",
@@ -40,7 +42,9 @@ var DefinitionType = map[string]string{
 	"_Bool":                  "int",
 	"size_t":                 "uint",
 	"ptrdiff_t":              "github.com/Konstantin8105/c4go/noarch.PtrdiffT",
-	"wchar_t":                "github.com/Konstantin8105/c4go/noarch.WcharT",
+	"github.com/Konstantin8105/c4go/noarch.PtrdiffT": "uint64",
+	"wchar_t": "github.com/Konstantin8105/c4go/noarch.WcharT",
+	"github.com/Konstantin8105/c4go/noarch.WcharT": "rune",
 
 	// void*
 	"void*":  "interface{}",
@@ -82,7 +86,12 @@ var DefinitionType = map[string]string{
 
 	// time.h
 	"clock_t": "github.com/Konstantin8105/c4go/noarch.ClockT",
+	"github.com/Konstantin8105/c4go/noarch.ClockT": "int64",
 
 	// signal.h
 	"sig_atomic_t": "int64",
+
+	// sys/types.h
+	"off_t":   "int64",
+	"__off_t": "int64",
 }
diff --git a/types/sizeof.go b/types/sizeof.go
index 903b255..de8f08a 100644
--- a/types/sizeof.go
+++ b/types/sizeof.go
@@ -138,7 +138,7 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 	case "short":
 		return 2, nil
 
-	case "int", "float":
+	case "int", "float", "long int":
 		return 4, nil
 
 	case "long", "double":
@@ -148,6 +148,40 @@ func SizeOf(p *program.Program, cType string) (size int, err error) {
 		return 16, nil
 	}
 
+	// definition type
+	if t, ok := program.DefinitionType[cType]; ok && cType != t {
+		return SizeOf(p, t)
+	}
+
+	// resolved type
+	conv := func(t string) (bytes int, ok bool) {
+		switch t {
+		case "byte", "int8", "uint8":
+			return 1, true
+
+		case "int16", "uint16":
+			return 2, true
+
+		case "int32", "uint32", "rune", "float32":
+			return 4, true
+
+		case "int64", "uint64", "float64", "complex64", "uintptr", "int", "uint":
+			return 8, true
+
+		case "complex128":
+			return 16, true
+		}
+		return -1, false
+	}
+	if t, ok := conv(cType); ok {
+		return t, nil
+	}
+	if r, err := ResolveType(p, cType); err != nil {
+		if t, ok := conv(r); ok {
+			return t, nil
+		}
+	}
+
 	// Get size for array types like: `base_type [count]`
 	totalArraySize := 1
 	arrayType, arraySize := GetArrayTypeAndSize(cType)


Test patch:
diff --git a/tests/unistd.c b/tests/unistd.c
index dc7cb85..4b295a3 100644
--- a/tests/unistd.c
+++ b/tests/unistd.c
@@ -1,8 +1,9 @@
-#include <unistd.h> 
-#include <sys/types.h>
 #include <fcntl.h>
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h> 
 #include "tests.h"
 
 #define START_TEST(t) \