Skip to content

Commit

Permalink
Fix assigning integers to pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Jul 24, 2023
1 parent 7767f69 commit ee21019
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
27 changes: 22 additions & 5 deletions source/ctod/cexpr.d
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,24 @@ bool ctodExpression(ref CtodCtx ctx, ref Node node) {
return false;
}

/// Fix assignment/initialization of node `r` to a node of type `lType`
///
/// C allows implicitly converting any T* -> U*, in D only T* -> `void*` gets that treatment
/// Try to fix this by adding explicit casts
/// C also allows converting numbers to pointers.
void convertPointerTypes(ref CtodCtx ctx, CType lType, ref Node r) {
CType rType = ctx.expType(r);

void castR() {
const castStr = "cast(" ~ lType.toString() ~ ") ";
if (!mayNeedParens(r)) {
r.prepend(castStr);
} else {
r.prepend(castStr ~ "(");
r.append(")");
}
}

if (lType.isPointer && rType.isPointer) {
if (lType.next[0] == rType.next[0]) {
return;
Expand All @@ -243,12 +257,15 @@ void convertPointerTypes(ref CtodCtx ctx, CType lType, ref Node r) {
// we don't want to simply cast away const with D string literals
return;
}
const castStr = "cast(" ~ lType.toString() ~ ") ";
if (!mayNeedParens(r)) {
r.prepend(castStr);
castR();
}
if (lType.isPointer && r.typeEnum == Sym.number_literal) {
if (r.source == "0") {
// int* x = 0; => int* x = null;
r.replace("null");
} else {
r.prepend(castStr ~ "(");
r.append(")");
// int* x = 3; => int* x = cast(int*) 3;
castR();
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions source/ctod/test.d
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ void main() {
}
");

// convert 0 to `null`
test(`void f() { FILE *f = 0; f = 0; f = 1; }`, `void f() { FILE* f = null; f = null; f = cast(FILE*) 1; }`);

test("typedef int intArr[3], intScalar;", "alias intArr = int[3];\nalias intScalar = int;");

test("typedef struct X X;", "");
Expand Down Expand Up @@ -739,6 +742,19 @@ double f(int count, ...)
}
`);

// TODO: prevent disappearing source code
test(`void main(void) {
if (1) {}
#if defined(SUPPORT_XXX)
else if (cmp(n, "XXX")) x = LoadX(n, 0);
#endif
}`, `void main() {
if (1) {}
version (SUPPORT_XXX) {
else if = LoadX(n, 0);
}
}`);

// tree-sitter doesn't parse this right, need to do manual preprocessing
version(none) test("
#ifdef __cplusplus
Expand Down
7 changes: 0 additions & 7 deletions source/ctod/translate.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import ctod.cdeclaration;
import ctod.cexpr;
import ctod.cpreproc;

// Enable switching to custom Associative Array type
version(none) {
import bops.ds.hashtable: Map = HashTable;
} else {
alias Map(K, V) = V[K];
}

import tree_sitter.api;

private immutable hasVersion = `
Expand Down
3 changes: 2 additions & 1 deletion source/ctod/tree_sitter.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module ctod.tree_sitter;

nothrow @safe:

// Enable switching to custom Associative Array type
version(none) {
import bops.ds.hashtable: Map = HashTable;
public import bops.ds.hashtable: Map = HashTable;
} else {
alias Map(K, V) = V[K];
}
Expand Down

0 comments on commit ee21019

Please sign in to comment.