Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)

private void VisitIndirectFieldDecl(IndirectFieldDecl indirectFieldDecl)
{
if (_config.ExcludeAnonymousFieldHelpers)
{
return;
}

if (IsPrevContextDecl<RecordDecl>(out var prevContext) && prevContext.IsAnonymousStructOrUnion)
{
// We shouldn't process indirect fields where the prev context is an anonymous record decl
Expand Down
15 changes: 15 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,21 @@ private void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr unaryExprOrT
if (IsPrevContextStmt<CallExpr>(out var callExpr))
{
var index = callExpr.Args.IndexOf(unaryExprOrTypeTraitExpr);

// If we didn't find the expression, try and find it under an implicit cast
if (index == -1)
{
for (int i = 0; i < callExpr.Args.Count; i++)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it would be better to write this as a single loop:

var args = callExpr.Args;
var index = -1;

for (int i = 0; i < args.Count; i++)
{
    var arg = args[i];

    if (IsStmtAsWritten(arg, unaryExprOrTypeTraitExpr))
    {
        index = i;
        break;
    }

    if (index == -1)
    {
        AddDiagnostic(DiagnosticLevel.Error, $"Failed to locate index of '{unaryExprOrTypeTraitExpr}' in callee declaration. Generated bindings may be incomplete.", calleeDecl);
    }
}

We can then have:

private bool IsStmtAsWritten(Stmt stmt, Stmt expectedStmt, bool removeParens = false)
{
    if (stmt == expectedStmt)
    {
        return true;
    }

    if (stmt is not Expr expr)
    {
        return false;
    }

    expr = GetExprAsWritten(expr, removeParens);
    return expr == expectedStmt;
}

This will make it easier to cover more cases in the future and to consistently look into an expr to remove implicit casts or other bits as necessary.

Copy link
Member

Choose a reason for hiding this comment

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

We have an existing IsStmtAsWritten that does a type check, rather than a value check, and it follows roughly this pattern.

{
var arg = callExpr.Args[i];
if (arg is ImplicitCastExpr implicitCastExpr && implicitCastExpr.SubExprAsWritten == unaryExprOrTypeTraitExpr)
{
index = i;
break;
}
}
}

var calleeDecl = callExpr.CalleeDecl;

if (calleeDecl is FunctionDecl functionDecl)
Expand Down