MochiLibraries / Biohazrd

A framework for automatically generating binding wrappers for C/C++ libraries
MIT License
60 stars 9 forks source link

Fix ambiguous overloads differing only by const-ness #154

Open PathogenDavid opened 3 years ago

PathogenDavid commented 3 years ago

Separate from const overloads, C++ also allows methods which differ only by the constness of parameters for some reason:

class MyClass
{
public:
    void MyFunction(void*);
    void MyFunction(const void*);
};

Discovered here in OpenCV:

https://github.com/opencv/opencv/blob/e250bae356f2a69026c8b24524fc0d768e180dbf/modules/flann/include/opencv2/flann/any.h#L48-L49

PathogenDavid commented 3 years ago

Wrote a test for this issue. Even though ConstOverloadRenameTransformation is for method const-ness, I think it makes sense to someone who isn't a C++ spec nerd to expect it to handle this case as well.

[Fact]
[RelatedIssue("https://github.com/InfectedLibraries/Biohazrd/issues/154")]
public void ConstParameterOverloads()
{
    TranslatedLibrary library = CreateLibrary
    (@"
void Test(int*);
void Test(const int*);
"
    );

    Assert.Equal("Test", library.Declarations[0].Name);
    Assert.Equal("Test", library.Declarations[1].Name);
    Assert.Equal(2, library.Declarations.Count);

    TranslatedLibrary transformed = new ConstOverloadRenameTransformation().Transform(library);

    TranslatedFunction function0 = Assert.IsType<TranslatedFunction>(transformed.Declarations[0]);
    TranslatedFunction function1 = Assert.IsType<TranslatedFunction>(transformed.Declarations[1]);
    Assert.NotEqual(function0.Name, function1.Name);
}

We should also add a test case for methods (as with the example in the OP.)

We should also check for this awful and contrived but valid case:

class MyClass
{
public:
    void Test(int*);
    void Test(const int*);
    void Test(int*) const;
    void Test(const int*) const;
};