Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't cast if macro parameter name represents a type #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion source/dpp/translation/macro_.d
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@ private auto fixCasts(R)(
&& tokens[scanIndex].spelling[0] == '.'
;

if(isType(tokens[i + 1 .. scanIndex - 1]) && !followedByDot) {
// make sure this is not a function call (look before the open paren)
const isFunctionParam =
i > 0
&& tokens[i - 1].kind == Token.Kind.Identifier;

if(isType(tokens[i + 1 .. scanIndex - 1])
&& !followedByDot
&& !isFunctionParam) {
middle ~= tokens[lastIndex .. i] ~
Token(Token.Kind.Punctuation, "cast(") ~
tokens[i + 1 .. scanIndex]; // includes closing paren
Expand Down
20 changes: 20 additions & 0 deletions tests/it/c/compile/preprocessor.d
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,23 @@ version(Posix) // FIXME
)
);
}

@("macro function's parameter name is a type")
@safe unittest {
shouldCompile(
C(
`
struct Foo {};
void actual_func(struct Foo*);
#define func(Foo) actual_func(Foo)
`
),
D(
q{
void actual_func(Foo *) {}
Foo *f;
func(f);
Copy link
Contributor Author

@cbecerescu cbecerescu May 21, 2020

Choose a reason for hiding this comment

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

Prior to this change, this would be preprocessed to "actual_func cast(f)" instead of "actual_func(f)"

}
)
);
}