Skip to content

Commit

Permalink
Simplify prefix stripping logic and consolidate
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikhr committed May 19, 2024
1 parent fa6653d commit 6703d6a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
18 changes: 8 additions & 10 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)

if (Config.StripEnumMemberTypeName)
{
Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*", RegexOptions.IgnoreCase);
var strippedName = stripParentNameRegex.Replace(escapedName, string.Empty);
escapedName = strippedName;
escapedName = PrefixAndStrip(escapedName, parentName, trimChar: '_');
}

var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator;
Expand Down Expand Up @@ -543,12 +541,12 @@ private void VisitFunctionDecl(FunctionDecl functionDecl)
if ((cxxMethodDecl is not null) && cxxMethodDecl.IsVirtual)
{
isVirtual = true;
escapedName = PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl));
escapedName = PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl));
}
else
{
isVirtual = false;
escapedName = EscapeAndStripName(name);
escapedName = EscapeAndStripMethodName(name);
}

var returnType = functionDecl.ReturnType;
Expand Down Expand Up @@ -2032,7 +2030,7 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD

var desc = new FunctionOrDelegateDesc {
AccessSpecifier = AccessSpecifier.Public,
EscapedName = EscapeAndStripName(name),
EscapedName = EscapeAndStripMethodName(name),
IsMemberFunction = true,
NativeTypeName = nativeTypeName,
HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen,
Expand Down Expand Up @@ -2120,7 +2118,7 @@ void OutputVtblEntry(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodDecl)
}

var remappedName = FixupNameForMultipleHits(cxxMethodDecl);
var escapedName = EscapeAndStripName(remappedName);
var escapedName = EscapeAndStripMethodName(remappedName);

var desc = new FieldDesc
{
Expand Down Expand Up @@ -2189,7 +2187,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
var desc = new FunctionOrDelegateDesc {
AccessSpecifier = AccessSpecifier.Public,
IsAggressivelyInlined = _config.GenerateAggressiveInlining,
EscapedName = EscapeAndStripName(name),
EscapedName = EscapeAndStripMethodName(name),
ParentName = parentName,
IsMemberFunction = true,
IsInherited = isInherited,
Expand Down Expand Up @@ -2269,7 +2267,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
{
body.Write("Marshal.GetDelegateForFunctionPointer<");
body.BeginMarker("delegate");
body.Write(PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl)));
body.Write(PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl)));
body.EndMarker("delegate");
body.Write(">(");
}
Expand All @@ -2278,7 +2276,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod
{
body.Write("lpVtbl->");
body.BeginMarker("vtbl", new KeyValuePair<string, object>("explicit", true));
body.Write(EscapeAndStripName(remappedName));
body.Write(EscapeAndStripMethodName(remappedName));
body.EndMarker("vtbl");
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ private void VisitDeclRefExpr(DeclRefExpr declRefExpr)

if (declRefExpr.Decl is FunctionDecl)
{
escapedName = EscapeAndStripName(name);
escapedName = EscapeAndStripMethodName(name);
}
}

Expand Down
33 changes: 24 additions & 9 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2326,13 +2326,9 @@ private static string EscapeName(string name)
}
}

private string EscapeAndStripName(string name)
private string EscapeAndStripMethodName(string name)
{
if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal))
{
name = name[_config.MethodPrefixToStrip.Length..];
}

name = PrefixAndStrip(name, _config.MethodPrefixToStrip);
return EscapeName(name);
}

Expand Down Expand Up @@ -6198,13 +6194,32 @@ private void ParenthesizeStmt(Stmt stmt)
}
}

private string PrefixAndStripName(string name, uint overloadIndex)
/// <summary>
/// Checks whether the specified name starts with a prefix, optionally trims a separator character following the prefix and returns the remainder.
/// </summary>
/// <param name="name">The name to strip.</param>
/// <param name="prefix">The prefix to strip from <paramref name="name"/>.</param>
/// <param name="trimChar">Additional separator that may follow <paramref name="prefix"/> which should also be trimmed away.</param>
/// <returns>
/// <paramref name="name"/> if it does not start with <paramref name="prefix"/>;
/// otherwise,
/// the remainder of <paramref name="name"/> after stripping <paramref name="prefix"/> and all immediate following occurences of <paramref name="trimChar"/> from it beginning.
/// </returns>
private static string PrefixAndStrip(string name, string prefix, char trimChar = '\0')
{
if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal))
var nameSpan = name.AsSpan();
if (nameSpan.StartsWith(prefix, StringComparison.Ordinal))
{
name = name[_config.MethodPrefixToStrip.Length..];
nameSpan = nameSpan[prefix.Length..];
nameSpan = nameSpan.TrimStart(trimChar);
return nameSpan.ToString();
}
return name;
}

private string PrefixAndStripMethodName(string name, uint overloadIndex)
{
name = PrefixAndStrip(name, _config.MethodPrefixToStrip);
return $"_{name}{((overloadIndex != 0) ? overloadIndex.ToString(CultureInfo.InvariantCulture) : "")}";
}

Expand Down

0 comments on commit 6703d6a

Please sign in to comment.