Skip to content

Commit

Permalink
Add unsafe modifier when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Nov 2, 2019
1 parent f5c4053 commit 0c76ae6
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, stri
var name = publicType.Name;

var isStruct = publicType.IsValueType && !publicType.IsPrimitive && !publicType.IsEnum;

var @readonly = isStruct && publicType.CustomAttributes.Any(a =>
a.AttributeType.FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute");

Expand All @@ -225,7 +225,7 @@ static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, stri
declarationName += "static ";

declarationName += name;

var declaration = new CodeTypeDeclaration(declarationName)
{
CustomAttributes = CreateCustomAttributes(publicType, excludeAttributes),
Expand Down Expand Up @@ -572,6 +572,9 @@ static void AddMethodToTypeDeclaration(CodeTypeDeclaration typeDeclaration, Meth

var returnType = CreateCodeTypeReference(member.ReturnType);

if (IsUnsafeSignatureType(member.ReturnType) || member.Parameters.Any(p => IsUnsafeSignatureType(p.ParameterType)))
returnType = MakeUnsafe(returnType);

var method = new CodeMemberMethod
{
Name = memberName,
Expand All @@ -586,6 +589,22 @@ static void AddMethodToTypeDeclaration(CodeTypeDeclaration typeDeclaration, Meth
typeDeclaration.Members.Add(method);
}

static bool IsUnsafeSignatureType(TypeReference typeReference)
{
while (true)
{
if (typeReference.IsPointer) return true;

if (typeReference.IsArray || typeReference.IsByReference)
{
typeReference = typeReference.GetElementType();
continue;
}

return false;
}
}

static bool IsExtensionMethod(ICustomAttributeProvider method)
{
return method.CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.ExtensionAttribute");
Expand Down Expand Up @@ -835,6 +854,9 @@ static void AddFieldToTypeDeclaration(CodeTypeDeclaration typeDeclaration, Field
var codeTypeReference = CreateCodeTypeReference(memberInfo.FieldType);
if (memberInfo.IsInitOnly)
codeTypeReference = MakeReadonly(codeTypeReference);
if (IsUnsafeSignatureType(memberInfo.FieldType))
codeTypeReference = MakeUnsafe(codeTypeReference);

var field = new CodeMemberField(codeTypeReference, memberInfo.Name)
{
Attributes = attributes,
Expand All @@ -852,6 +874,11 @@ static CodeTypeReference MakeReadonly(CodeTypeReference typeReference)
return ModifyCodeTypeReference(typeReference, "readonly");
}

static CodeTypeReference MakeUnsafe(CodeTypeReference typeReference)
{
return ModifyCodeTypeReference(typeReference, "unsafe");
}

static CodeTypeReference ModifyCodeTypeReference(CodeTypeReference typeReference, string modifier)
{
using (var provider = new CSharpCodeProvider())
Expand Down

0 comments on commit 0c76ae6

Please sign in to comment.