Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions source/dpp/translation/macro_.d
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,11 @@ private auto fixCasts(R)(

if( // const type
tokens.length >= 2
&& tokens[0] == Token(Token.Kind.Keyword, "const")
&& isType(tokens[1..$])
&& ((tokens[0] == Token(Token.Kind.Keyword, "const")
&& isType(tokens[1..$]))
|| (tokens[$-1] == Token(Token.Kind.Keyword, "const")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sure that we treat all the cases with const + pointers, and view them as casts.

However, DPP does not currently translate the C cast within a macro. So that means that "(struct foo const *)" or "(struct foo * const)" are not going to be translated to valid D types (although now they are at least going to be seen as casts)

&& isType(tokens[0..$-1]))
)
)
return true;

Expand All @@ -377,13 +380,22 @@ private auto fixCasts(R)(
)
return true;

if ( // macro attribute (e.g. __force) + type
if( // e.g. (struct foo) var
tokens.length == 2
&& tokens[0] == Token(Token.Kind.Keyword, "struct")
&& tokens[1].kind == Token.Kind.Identifier
// not checking for user defined type (foo might be given as parameter)
)
return true;

if( // macro attribute (e.g. __force) + type
tokens.length >= 2
&& tokens[0].kind == Token.Kind.Identifier
&& isType(tokens[1..$])
)
return true;


return false;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/it/c/compile/preprocessor.d
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ import it;
}


@("user-defined struct cast in macro")
@safe unittest {
shouldCompile(
C(
`
struct foo { int bar; };
#define PTR(st, vr) ((struct st *) &vr)
#define PTR_CONST1(st, vr) ((const struct st *) &vr)
#define PTR_CONST2(st, vr) ((struct st const *) &vr)
#define PTR_CONST3(st, vr) ((struct st * const) &vr)
`
),
D(
q{
foo f;
auto a = PTR(foo, f);
auto b = PTR_CONST1(foo, f);

// DPP does not currently translate those "macro params"
// i.e. leaves "struct foo const *" unchanged
// auto c = PTR_CONST2(foo, f);
// auto d = PTR_CONST3(foo, f);
}
)
);
}


version(Posix) // FIXME
@("multiline")
@safe unittest {
Expand Down