Skip to content

Commit

Permalink
Fix function pointers parsing passed as function argument type
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed May 12, 2024
1 parent 1f9e3c0 commit c85a4c1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
39 changes: 39 additions & 0 deletions src/CppAst.Tests/TestFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,44 @@ public void TestFunctionTemplate()
);
}


[Test]
public void TestFunctionPointersByParam()
{
ParseAssert(@"
void function0(int a, int b, float (*callback)(void*, double));
",
compilation =>
{
Assert.False(compilation.HasErrors);

Assert.AreEqual(1, compilation.Functions.Count);

{
var cppFunction = compilation.Functions[0];
Assert.AreEqual("function0", cppFunction.Name);
Assert.AreEqual(3, cppFunction.Parameters.Count);

Assert.IsInstanceOf<CppPointerType>(cppFunction.Parameters[2].Type);
var pointerType = (CppPointerType)cppFunction.Parameters[2].Type;
Assert.IsInstanceOf<CppFunctionType>(pointerType.ElementType);
var functionType = (CppFunctionType)pointerType.ElementType;
Assert.AreEqual(2, functionType.Parameters.Count);
Assert.AreEqual("float", functionType.ReturnType.ToString());
Assert.AreEqual("void *", functionType.Parameters[0].Type.ToString());
Assert.AreEqual("double", functionType.Parameters[1].Type.ToString());


Assert.AreEqual("void", cppFunction.ReturnType.ToString());

var cppFunction1 = compilation.FindByName<CppFunction>("function0");
Assert.AreEqual(cppFunction, cppFunction1);
}
}
);
}



}
}
10 changes: 5 additions & 5 deletions src/CppAst/CppModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ private CppFunction VisitFunctionDecl(CXCursor cursor, CXCursor parent, void* da
case CXCursorKind.CXCursor_ParmDecl:
var argName = CXUtil.GetCursorSpelling(argCursor);

var parameter = new CppParameter(GetCppType(argCursor.Type.Declaration, argCursor.Type, functionCursor, clientData), argName);
var parameter = new CppParameter(GetCppType(argCursor.Type.Declaration, argCursor.Type, argCursor, clientData), argName);

cppFunction.Parameters.Add(parameter);

Expand Down Expand Up @@ -1939,12 +1939,12 @@ private CppFunctionType VisitFunctionType(CXCursor cursor, CXType type, CXCursor
// }

bool isParsingParameter = false;
parent.VisitChildren((cxCursor, parent1, clientData) =>
parent.VisitChildren((argCursor, functionCursor, clientData) =>
{
if (cxCursor.Kind == CXCursorKind.CXCursor_ParmDecl)
if (argCursor.Kind == CXCursorKind.CXCursor_ParmDecl)
{
var name = CXUtil.GetCursorSpelling(cxCursor);
var parameterType = GetCppType(cxCursor.Type.Declaration, cxCursor.Type, cxCursor, data);
var name = CXUtil.GetCursorSpelling(argCursor);
var parameterType = GetCppType(argCursor.Type.Declaration, argCursor.Type, argCursor, data);

cppFunction.Parameters.Add(new CppParameter(parameterType, name));
isParsingParameter = true;
Expand Down

0 comments on commit c85a4c1

Please sign in to comment.