Skip to content
Open
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
21 changes: 13 additions & 8 deletions sources/libClangSharp/ClangSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#endif

#include <clang/Basic/SourceManager.h>
#include <clang/Basic/TargetInfo.h>

#ifdef _MSC_VER
#pragma warning(pop)
Expand Down Expand Up @@ -108,14 +109,18 @@ int64_t getVtblIdx(const GlobalDecl& d)
VTableContextBase* VTC = CMD->getASTContext().getVTableContext();

if (MicrosoftVTableContext* MSVTC = dyn_cast<MicrosoftVTableContext>(VTC)) {
MethodVFTableLocation ML = MSVTC->getMethodVFTableLocation(d);

// clang 22's Microsoft vftable layout reports Index == 0 for a virtual destructor
// regardless of its declaration position, so it collides with whatever occupies slot 0
// whenever the destructor is not declared first. The vftable layout itself is ordered
// correctly, so recover the true slot by counting the function-pointer components that
// precede the deleting-destructor component (non-slot components such as offsets are
// skipped so the count stays in the same space as MethodVFTableLocation::Index).
GlobalDecl method = d;

if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(CMD)) {
ASTContext& context = CMD->getASTContext();
CXXDtorType dtorType = context.getTargetInfo().emitVectorDeletingDtors(context.getLangOpts()) ? Dtor_VectorDeleting : Dtor_Deleting;
method = GlobalDecl(DD, dtorType);
}

MethodVFTableLocation ML = MSVTC->getMethodVFTableLocation(method);

// Recover the destructor slot from the layout, counting only function-pointer
// components so the count stays in the same space as MethodVFTableLocation::Index.
if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(CMD)) {
const VTableLayout& layout = MSVTC->getVFTableLayout(RD, ML.VFPtrOffset);
int64_t slot = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Globalization;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests;

[TestFixture]
public sealed class LargeVirtualMethodCountWithDestructorTest : PInvokeGeneratorTest
{
[TestCase(190, false)]
[TestCase(191, false)]
[TestCase(192, false)]
[TestCase(191, true)]
public async Task GeneratesAllVirtualMethods(int methodCount, bool destructorLast)
{
var input = new StringBuilder("class Example { public:\n");

if (!destructorLast)
{
_ = input.AppendLine("virtual ~Example() {}");
}

for (var i = 0; i < methodCount; i++)
{
_ = input.AppendLine(CultureInfo.InvariantCulture, $"virtual bool Method{i}() = 0;");
}

if (destructorLast)
{
_ = input.AppendLine("virtual ~Example() {}");
}

_ = input.AppendLine("};");

var actual = await GenerateBindingsAsync(
inputContents: input.ToString(),
outputMode: PInvokeGeneratorOutputMode.CSharp,
configOptions: PInvokeGeneratorConfigurationOptions.None,
excludedNames: null,
remappedNames: null,
withAccessSpecifiers: null,
withAttributes: null,
withCallConvs: null,
withClasses: null,
withLibraryPaths: null,
withNamespaces: null,
withSetLastErrors: null,
withTransparentStructs: null,
withTypes: null,
withUsings: null,
withPackings: null,
expectedDiagnostics: null,
libraryPath: DefaultLibraryPath,
commandLineArgs: null,
language: "c++",
languageStandard: DefaultCppStandard).ConfigureAwait(false);

Assert.That(actual, Does.Contain("public void Dispose()"));

for (var i = 0; i < methodCount; i++)
{
Assert.That(actual, Does.Contain($"public bool Method{i}()"));
}
}
}
Loading