Repository: Konstantin8105/c4go
Pull request #349 (2019-03-21 06:32:57)

solve problem with variables restrict

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=h1) Report
> Merging [#349](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/e2d3bcf7a5f94184dc430afcfe8da155c19f436d?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/349/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #349      +/-   ##
==========================================
- Coverage   86.47%   86.45%   -0.02%     
==========================================
  Files         186      186              
  Lines       10630    10639       +9     
==========================================
+ Hits         9192     9198       +6     
- Misses       1026     1027       +1     
- Partials      412      414       +2
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [types/resolve.go](https://codecov.io/gh/Konstantin8105/c4go/pull/349/diff?src=pr&el=tree#diff-dHlwZXMvcmVzb2x2ZS5nbw==) | `76.32% <33.33%> (+0.31%)` | :arrow_up: |
| [util/util.go](https://codecov.io/gh/Konstantin8105/c4go/pull/349/diff?src=pr&el=tree#diff-dXRpbC91dGlsLmdv) | `83.71% <73.33%> (-1.13%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=footer). Last update [e2d3bcf...3c7eea8](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=h1) Report
> Merging [#349](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/e2d3bcf7a5f94184dc430afcfe8da155c19f436d?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `66.66%`.

[![Impacted file tree graph](https://codecov.io/gh/Konstantin8105/c4go/pull/349/graphs/tree.svg?width=650&token=esH2rHXlZ2&height=150&src=pr)](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #349      +/-   ##
==========================================
- Coverage   86.47%   86.45%   -0.02%     
==========================================
  Files         186      186              
  Lines       10630    10639       +9     
==========================================
+ Hits         9192     9198       +6     
- Misses       1026     1027       +1     
- Partials      412      414       +2
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [types/resolve.go](https://codecov.io/gh/Konstantin8105/c4go/pull/349/diff?src=pr&el=tree#diff-dHlwZXMvcmVzb2x2ZS5nbw==) | `76.32% <33.33%> (+0.31%)` | :arrow_up: |
| [util/util.go](https://codecov.io/gh/Konstantin8105/c4go/pull/349/diff?src=pr&el=tree#diff-dXRpbC91dGlsLmdv) | `83.71% <73.33%> (-1.13%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=footer). Last update [e2d3bcf...3c7eea8](https://codecov.io/gh/Konstantin8105/c4go/pull/349?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/scripts/ed.sh b/scripts/ed.sh
index 6e4e570..0a43308 100755
--- a/scripts/ed.sh
+++ b/scripts/ed.sh
@@ -28,7 +28,7 @@ mkdir -p ./testdata/
 
 # tranpilation
 $C4GO transpile  -cpuprofile=./testdata/cpu.out              \
-				 -s                                          \
+				 -s 										 \
 	             -o="$TEMP_FOLDER/$VERSION.go"               \
 				 -clang-flag="-DPROGVERSION=\"$VERSION\""    \
 				 $TEMP_FOLDER/$VERSION/$VERSION/*.c
diff --git a/types/resolve.go b/types/resolve.go
index a6e8c05..5a56db5 100644
--- a/types/resolve.go
+++ b/types/resolve.go
@@ -259,14 +259,13 @@ func ResolveType(p *program.Program, s string) (resolveResult string, err error)
 	if s[len(s)-1] == '*' {
 		r := strings.TrimSpace(s[:len(s)-1])
 		r, err = ResolveType(p, r)
-		if err != nil {
-			err = fmt.Errorf("Cannot resolve star `*` for %v : %v", s, err)
-			return s, err
-		}
 		prefix := "[]"
 		if strings.Contains(r, "noarch.File") {
 			prefix = "*"
 		}
+		if err != nil {
+			err = fmt.Errorf("Cannot resolve star `*` for %v : %v", s, err)
+		}
 		return prefix + r, err
 	}
 
@@ -325,7 +324,7 @@ func ResolveType(p *program.Program, s string) (resolveResult string, err error)
 
 	errMsg := fmt.Sprintf(
 		"I couldn't find an appropriate Go type for the C type '%s'.", s)
-	return "interface{}", errors.New(errMsg)
+	return s, errors.New(errMsg)
 }
 
 // resolveType determines the Go type from a C type.
diff --git a/util/util.go b/util/util.go
index db211a7..b265df8 100644
--- a/util/util.go
+++ b/util/util.go
@@ -322,14 +322,36 @@ func CleanCType(s string) (out string) {
 	out = strings.Replace(out, "( *)", "(*)", -1)
 
 	// Remove any whitespace or attributes that are not relevant to Go.
-	out = strings.Replace(out, "const", "", -1)
-	out = strings.Replace(out, "volatile", "", -1)
-	out = strings.Replace(out, "__restrict", "", -1)
-	out = strings.Replace(out, "restrict", "", -1)
-	out = strings.Replace(out, "_Nullable", "", -1)
 	out = strings.Replace(out, "\t", "", -1)
 	out = strings.Replace(out, "\n", "", -1)
 	out = strings.Replace(out, "\r", "", -1)
+	list := []string{"const", "volatile", "__restrict", "restrict", "_Nullable"}
+	for _, word := range list {
+		// examples :
+		// `const char  * *`
+		// `const struct parg_option`
+		// `void (*)(int  *, void  *, const char  *)`
+		out = strings.Replace(out, " "+word+" ", "", -1)
+		out = strings.Replace(out, " "+word+"*", "*", -1)
+		out = strings.Replace(out, "*"+word+" ", "*", -1)
+		out = strings.Replace(out, "*"+word+"*", "* *", -1)
+
+		if pr := word + " "; strings.HasPrefix(out, pr) {
+			out = out[len(word):]
+		}
+		if po := " " + word; strings.HasSuffix(out, po) {
+			out = out[:len(out)-len(word)]
+		}
+
+		if pr := word + "*"; strings.HasPrefix(out, pr) {
+			out = out[len(word):]
+		}
+		if po := "*" + word; strings.HasSuffix(out, po) {
+			out = out[:len(out)-len(word)]
+		}
+
+		out = strings.TrimSpace(out)
+	}
 
 	// remove space from pointer symbols
 	out = strings.Replace(out, "* *", "**", -1)


Test patch:
diff --git a/ast/var_decl_test.go b/ast/var_decl_test.go
index 74b4d5c..5efe93b 100644
--- a/ast/var_decl_test.go
+++ b/ast/var_decl_test.go
@@ -277,6 +277,24 @@ func TestVarDecl(t *testing.T) {
 			IsTls:        false,
 			ChildNodes:   []Node{},
 		},
+		`0x31ad060 <line:70:1, col:26> col:13 used restricted 'bool':'enum Bool' static cinit`: &VarDecl{
+			Addr:         0x31ad060,
+			Pos:          NewPositionFromString("line:70:1, col:26"),
+			Position2:    "col:13",
+			Name:         "restricted",
+			Type:         "bool",
+			Type2:        "enum Bool",
+			IsExtern:     false,
+			IsUsed:       true,
+			IsCInit:      true,
+			IsCallInit:   false,
+			IsNrvo:       false,
+			IsReferenced: false,
+			IsStatic:     true,
+			IsRegister:   false,
+			IsTls:        false,
+			ChildNodes:   []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)
diff --git a/tests/array.c b/tests/array.c
index 64e6d32..3e2f0a9 100644
--- a/tests/array.c
+++ b/tests/array.c
@@ -662,6 +662,19 @@ void test_double_array()
     // TODO : view_matrix(p,4,3);
 }
 
+static void trans(char * p)
+{
+	printf("trans = `%s`\n",p);
+}
+
+void test_func_byte()
+{
+	char * const gameOver = "game over";
+	trans(gameOver);
+	char * gameOver2 = "game over";
+	trans(gameOver2);
+}
+
 int main()
 {
     plan(184);
@@ -1200,6 +1213,7 @@ int main()
     }
     START_TEST(string_array);
     START_TEST(double_array);
+	START_TEST(func_byte);
 
     done_testing();
 }
diff --git a/tests/operators.c b/tests/operators.c
index 0b0395b..5456d36 100644
--- a/tests/operators.c
+++ b/tests/operators.c
@@ -62,9 +62,21 @@ void view(int c)
     printf("%d\n", c);
 }
 
+enum Bool { false = 0, true = 1 };
+typedef enum Bool bool;
+static bool valGlobBool     = true;
+static int valGlobInt       = 42;
+static double valGlobDouble = 45.0;
+static bool restricted      = true;
+
 int main()
 {
-    plan(119);
+    plan(123);
+
+	is_eq(valGlobInt, 42);
+	is_eq(valGlobDouble, 45);
+	is_eq(valGlobBool, 1);
+	is_eq(restricted, 1);
 
     int i = 10;
     signed char j = 1;