Skip to content

Commit

Permalink
Fix additional low hanging bugs (#257)
Browse files Browse the repository at this point in the history
* Refactoring how the calling convention is looked up for a given cursor or type

* Ensure that non-char based string literals don't assert with debug clang

* Ensure that managled names are correctly represented in DllImport

* Ensure creating a span over a pointer works on netstandard

* Don't include the underscore prefix on the entry point name for simple C exports
  • Loading branch information
tannergooding authored Aug 4, 2021
1 parent d0ca6a6 commit 69bf841
Show file tree
Hide file tree
Showing 33 changed files with 808 additions and 218 deletions.
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

0 comments on commit 69bf841

Please sign in to comment.