Skip to content

Commit

Permalink
char array initialization. Fixes #551 and #552 (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin8105 authored and elliotchance committed Jan 14, 2018
1 parent d752932 commit dea71c3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
13 changes: 12 additions & 1 deletion tests/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
test_##t();

// size of that file
int filesize = 9699;
int filesize = 9979;

void test_putchar()
{
Expand Down Expand Up @@ -46,6 +46,17 @@ void test_printf()
printf("# Width trick: %*d \n", 5, 10);
printf("# %s \n", "A string");


int magnitude = 4;
char printfFormat[30] = "%0";
char magnitudeString[10];
sprintf(magnitudeString, "%d", magnitude);
strcat(printfFormat, magnitudeString);
strcat(printfFormat, "d ");
printf("# ");
printf(printfFormat, 120);
printf(" \n");

pass("%s", "printf");
}

Expand Down
15 changes: 14 additions & 1 deletion transpiler/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package transpiler

import (
"bytes"
"fmt"
"go/token"

Expand All @@ -13,6 +14,7 @@ import (

"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/types"
"github.com/elliotchance/c2go/util"
)

Expand All @@ -21,8 +23,19 @@ func transpileFloatingLiteral(n *ast.FloatingLiteral) *goast.BasicLit {
}

func transpileStringLiteral(n *ast.StringLiteral) goast.Expr {
// Example:
// StringLiteral 0x280b918 <col:29> 'char [30]' lvalue "%0"
s, err := types.GetAmountArraySize(n.Type)
if err != nil {
return util.NewCallExpr("[]byte",
util.NewStringLit(strconv.Quote(n.Value+"\x00")))
}
buf := bytes.NewBufferString(n.Value + "\x00")
if buf.Len() < s {
buf.Write(make([]byte, s-buf.Len()))
}
return util.NewCallExpr("[]byte",
util.NewStringLit(strconv.Quote(n.Value+"\x00")))
util.NewStringLit(strconv.Quote(buf.String())))
}

func transpileIntegerLiteral(n *ast.IntegerLiteral) *goast.BasicLit {
Expand Down
24 changes: 24 additions & 0 deletions types/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"errors"
"fmt"
"strconv"
"strings"

"github.com/elliotchance/c2go/program"
Expand Down Expand Up @@ -455,3 +456,26 @@ func GenerateCorrectType(name string) string {

return out
}

// GetAmountArraySize - return amount array size
// Example :
// In : 'char [40]'
// Out : 40
func GetAmountArraySize(cType string) (size int, err error) {
reg := util.GetRegex("\\[(?P<size>\\d+)\\]")
match := reg.FindStringSubmatch(cType)

if reg.NumSubexp() != 1 {
err = fmt.Errorf("Cannot found size of array in type : %s", cType)
return
}

result := make(map[string]string)
for i, name := range reg.SubexpNames() {
if i != 0 {
result[name] = match[i]
}
}

return strconv.Atoi(result["size"])
}

0 comments on commit dea71c3

Please sign in to comment.