Skip to content

Commit

Permalink
Don't split multi-var declaration with same type
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Nov 1, 2022
1 parent bab59b4 commit 58a9e1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 11 additions & 1 deletion source/ctod/cdeclaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ bool ctodTryDeclaration(ref CtodCtx ctx, ref Node node) {
foreach(s; inlinetypes) {
result ~= s.toString();
}
CType previousType = CType.none;
foreach(d; decls) {
// `char x;` => `char x = 0;`
if (cInit && d.initializer.length == 0 && noZeroInitInD(d.type)) {
d.initializer = "0";
}
result ~= d.toString() ~ suffix;
if (d.type != previousType) {
if (previousType != CType.none) {
result ~= "; ";
}
result ~= d.toString();
} else {
result ~= ", " ~ d.identifier ~ d.initializerAssign;
}
ctx.registerDecl(d);
previousType = d.type;
}
result ~= suffix;
node.replace(result);
return true;
}
Expand Down
12 changes: 8 additions & 4 deletions source/ctod/ctype.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ pure nothrow:
result ~= " ";
result ~= identifier;
}
if (initializer.length > 0) {
result ~= " = ";
result ~= initializer;
}
result ~= this.initializerAssign();
}
return result;
}

string initializerAssign() const {
if (initializer.length > 0) {
return " = " ~ initializer;
}
return "";
}

bool opEquals(const Decl other) const scope {
return storageClasses == other.storageClasses && type == other.type
&& identifier == other.identifier && initializer == other.initializer;
Expand Down
12 changes: 10 additions & 2 deletions source/ctod/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ version(none)
test("unsigned x[10];", "uint[10] x;");
test("struct S { unsigned x; };", "struct S { uint x; }");

test("int x = 1, y = 2, *z = null, w = 0;", "int x = 1, y = 2; int* z = null; int w = 0;");
test("unsigned *in = i, *out = o;", "uint* in_ = i, out_ = o;");
test("unsigned *in = i, *out = o;", "uint* in_ = i, out_ = o;");
test(
"void f() {for(int i = 1, j = 2; i < 5; ++i);}",
"void f() {for(int i = 1, j = 2; i < 5; ++i){}}"
);

// inline struct/enum
test("void foo(struct {int x;} t);", "struct _T {int x;}void foo(_T t);");
test("void foo(struct T t);", "void foo(T t);");
Expand All @@ -67,7 +75,7 @@ version(none)
test("int *a4[6][7];", "int*[7][6] a4;");
test("int *(*a5)[8][9];", "int*[9][8]* a5;");
test("int **a6[8][9];", "int**[9][8] a6;");
test("int a[2], (*b)[2];", "int[2] a;int[2]* b;");
test("int a[2], (*b)[2];", "int[2] a; int[2]* b;");
test("double z[4][3];", "double[3][4] z = 0;");
test(
"void (*f0)(int x, float, char*, char[], char*[], char***);",
Expand Down Expand Up @@ -139,7 +147,7 @@ inline static void foo(int x[], int (*y)(void)) {
}
", "
pragma(inline, true) private void foo(int* x, int function() y) {
int* z;int y;
int* z; int y;
}
");

Expand Down

0 comments on commit 58a9e1a

Please sign in to comment.