Repository: Konstantin8105/c4go
Pull request #302 (2019-03-04 12:36:07)

ast: add type2 for StringLiteral

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

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

```diff
@@            Coverage Diff             @@
##           master     #302      +/-   ##
==========================================
- Coverage   86.79%   86.77%   -0.02%     
==========================================
  Files         184      184              
  Lines       10071    10079       +8     
==========================================
+ Hits         8741     8746       +5     
- Misses        922      925       +3     
  Partials      408      408
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/302?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/literals.go](https://codecov.io/gh/Konstantin8105/c4go/pull/302/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9saXRlcmFscy5nbw==) | `78.26% <0%> (-3.56%)` | :arrow_down: |
| [ast/string\_literal.go](https://codecov.io/gh/Konstantin8105/c4go/pull/302/diff?src=pr&el=tree#diff-YXN0L3N0cmluZ19saXRlcmFsLmdv) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/302?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/302?src=pr&el=footer). Last update [f79c4b1...fa69c62](https://codecov.io/gh/Konstantin8105/c4go/pull/302?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/302?src=pr&el=h1) Report
> Merging [#302](https://codecov.io/gh/Konstantin8105/c4go/pull/302?src=pr&el=desc) into [master](https://codecov.io/gh/Konstantin8105/c4go/commit/f79c4b1904e7e6739e2f1f075d78d9021a6faf01?src=pr&el=desc) will **decrease** coverage by `0.01%`.
> The diff coverage is `53.84%`.

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

```diff
@@            Coverage Diff             @@
##           master     #302      +/-   ##
==========================================
- Coverage   86.79%   86.77%   -0.02%     
==========================================
  Files         184      184              
  Lines       10071    10079       +8     
==========================================
+ Hits         8741     8746       +5     
- Misses        922      925       +3     
  Partials      408      408
```


| [Impacted Files](https://codecov.io/gh/Konstantin8105/c4go/pull/302?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [transpiler/literals.go](https://codecov.io/gh/Konstantin8105/c4go/pull/302/diff?src=pr&el=tree#diff-dHJhbnNwaWxlci9saXRlcmFscy5nbw==) | `78.26% <0%> (-3.56%)` | :arrow_down: |
| [ast/string\_literal.go](https://codecov.io/gh/Konstantin8105/c4go/pull/302/diff?src=pr&el=tree#diff-YXN0L3N0cmluZ19saXRlcmFsLmdv) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Konstantin8105/c4go/pull/302?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/302?src=pr&el=footer). Last update [f79c4b1...fa69c62](https://codecov.io/gh/Konstantin8105/c4go/pull/302?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/ast/string_literal.go b/ast/string_literal.go
index effea99..ed23cdd 100644
--- a/ast/string_literal.go
+++ b/ast/string_literal.go
@@ -5,6 +5,7 @@ type StringLiteral struct {
 	Addr       Address
 	Pos        Position
 	Type       string
+	Type2      string
 	Value      string
 	Runes      bool
 	IsLvalue   bool
@@ -13,14 +14,19 @@ type StringLiteral struct {
 
 func parseStringLiteral(line string) *StringLiteral {
 	groups := groupsFromRegex(
-		`<(?P<position>.*)> '(?P<type>.*)' lvalue (?P<runes>L)?(?P<value>".*")`,
+		`<(?P<position>.*)>
+		 '(?P<type1>.*?)'(:'(?P<type2>.*)')?
+		(?P<lvalue> lvalue)?
+		 (?P<runes>L)?
+		(?P<value>".*")`,
 		line,
 	)
 
 	return &StringLiteral{
 		Addr:       ParseAddress(groups["address"]),
 		Pos:        NewPositionFromString(groups["position"]),
-		Type:       groups["type"],
+		Type:       groups["type1"],
+		Type2:      groups["type2"],
 		Value:      unquote(groups["value"]),
 		IsLvalue:   true,
 		Runes:      groups["runes"] != "",
diff --git a/transpiler/literals.go b/transpiler/literals.go
index b1c616e..2e9404c 100644
--- a/transpiler/literals.go
+++ b/transpiler/literals.go
@@ -63,9 +63,13 @@ func transpileStringLiteral(p *program.Program, n *ast.StringLiteral, arrayToArr
 	if baseType != "char" &&
 		!strings.Contains(baseType, "int") &&
 		!strings.Contains(baseType, "wchar_t") {
-		err = fmt.Errorf("Type is not valid : %v", n.Type)
-		p.AddMessage(p.GenerateWarningMessage(err, n))
-		return
+		if t, ok := p.TypedefType[baseType]; ok {
+			n.Type = t
+		} else {
+			err = fmt.Errorf("Type is not valid : `%v`", n.Type)
+			p.AddMessage(p.GenerateWarningMessage(err, n))
+			return
+		}
 	}
 	var s int
 	s, err = types.GetAmountArraySize(n.Type)


Test patch:
diff --git a/ast/string_literal_test.go b/ast/string_literal_test.go
index 755215f..bd0e4e9 100644
--- a/ast/string_literal_test.go
+++ b/ast/string_literal_test.go
@@ -40,6 +40,16 @@ func TestStringLiteral(t *testing.T) {
 			Value:      "copy successful",
 			ChildNodes: []Node{},
 		},
+		`0x2759028 <col:19> 'STRING':'char [100]' lvalue "C"`: &StringLiteral{
+			Addr:       0x2759028,
+			Pos:        NewPositionFromString("col:19"),
+			Type:       "STRING",
+			Type2:      "char [100]",
+			IsLvalue:   true,
+			Runes:      false,
+			Value:      "C",
+			ChildNodes: []Node{},
+		},
 	}
 
 	runNodeTests(t, nodes)