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

Fix additional low hanging bugs #257

Merged
merged 5 commits into from
Aug 4, 2021
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
12 changes: 12 additions & 0 deletions sources/ClangSharp.Interop/Extensions/CXModuleMapDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,19 @@ public Span<byte> WriteToBuffer(uint options, out CXErrorCode errorCode)
{
sbyte* pBuffer; uint size;
errorCode = clang.ModuleMapDescriptor_writeToBuffer(this, options, &pBuffer, &size);

#if NETSTANDARD
var result = new byte[checked((int)size)];

fixed (byte* pResult = result)
{
Buffer.MemoryCopy(pBuffer, pResult, size, size);
}

return result;
#else
return new Span<byte>(pBuffer, (int)size);
#endif
}
}
}
13 changes: 13 additions & 0 deletions sources/ClangSharp.Interop/Extensions/CXTranslationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,20 @@ public Span<CXToken> Tokenize(CXSourceRange sourceRange)
{
CXToken* pTokens; uint numTokens;
clang.tokenize(this, sourceRange, &pTokens, &numTokens);

#if NETSTANDARD
var result = new CXToken[checked((int)numTokens)];

fixed (CXToken* pResult = result)
{
var size = sizeof(CXToken) * numTokens;
Buffer.MemoryCopy(pTokens, pResult, size, size);
}

return result;
#else
return new Span<CXToken>(pTokens, (int)numTokens);
#endif
}

public override string ToString() => Spelling.ToString();
Expand Down
12 changes: 12 additions & 0 deletions sources/ClangSharp.Interop/Extensions/CXVirtualFileOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,19 @@ public Span<byte> WriteToBuffer(uint options, out CXErrorCode errorCode)
{
sbyte* pBuffer; uint size;
errorCode = clang.VirtualFileOverlay_writeToBuffer(this, options, &pBuffer, &size);

#if NETSTANDARD
var result = new byte[checked((int)size)];

fixed (byte* pResult = result)
{
Buffer.MemoryCopy(pBuffer, pResult, size, size);
}

return result;
#else
return new Span<byte>(pBuffer, (int)size);
#endif
}
}
}
32 changes: 8 additions & 24 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,28 +452,16 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
}

var type = functionDecl.Type;
var callConv = CXCallingConv.CXCallingConv_Invalid;
var callingConventionName = GetCallingConvention(functionDecl, cxxRecordDecl, type);

if (type is AttributedType attributedType)
{
type = attributedType.ModifiedType;
callConv = attributedType.Handle.FunctionTypeCallingConv;
}
var isDllImport = body is null && !isVirtual;
var entryPoint = isDllImport ? functionDecl.Handle.Mangling.CString : null;

if (type is FunctionType functionType)
if (entryPoint == $"_{functionDecl.Name}")
{
if (callConv == CXCallingConv.CXCallingConv_Invalid)
{
callConv = functionType.CallConv;
}
entryPoint = functionDecl.Name;
}

var callingConventionName = GetCallingConvention(functionDecl, callConv, name);
var entryPoint = !isVirtual && body is null
? (cxxMethodDecl is null) ? GetCursorName(functionDecl) : cxxMethodDecl.Handle.Mangling.CString
: null;
var isDllImport = body is null && !isVirtual;

var needsReturnFixup = isVirtual && NeedsReturnFixup(cxxMethodDecl);

var desc = new FunctionOrDelegateDesc<(string Name, PInvokeGenerator This)>
Expand Down Expand Up @@ -2435,14 +2423,10 @@ void ForFunctionProtoType(TypedefDecl typedefDecl, FunctionProtoType functionPro
var name = GetRemappedCursorName(typedefDecl);
var escapedName = EscapeName(name);

var callingConventionName = GetCallingConvention(typedefDecl,
(parentType is AttributedType)
? parentType.Handle.FunctionTypeCallingConv
: functionProtoType.CallConv, name);
var callingConventionName = GetCallingConvention(typedefDecl, context: null, typedefDecl.TypeForDecl);

var returnType = functionProtoType.ReturnType;
var returnTypeName =
GetRemappedTypeName(typedefDecl, context: null, returnType, out var nativeTypeName);
var returnType = functionProtoType.ReturnType;
var returnTypeName = GetRemappedTypeName(typedefDecl, context: null, returnType, out var nativeTypeName);

StartUsingOutputBuilder(name);
{
Expand Down
Loading