MochiLibraries / Biohazrd

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

Avoid unnecessary out-of-scope file promotion when the cursor results in no declaration #172

Open PathogenDavid opened 3 years ago

PathogenDavid commented 3 years ago

Out-of-scope file promotion happens eagerly in TranslationUnitParser.CreateDeclarations:

https://github.com/InfectedLibraries/Biohazrd/blob/99a504b77c076651ade931aa168ce7359adc0a4a/Biohazrd/TranslationUnitParser.cs#L251-L253

An unfortunate side-effect of this is if the cursor results in no translated declarations, the file is still promoted and the warning is still emitted.

This was discovered with this workaround (see https://github.com/InfectedLibraries/Biohazrd/issues/171 for some background.)

PathogenDavid commented 3 years ago

Repro test for ScopeTests:

        [Fact]
        [RelatedIssue("https://github.com/InfectedLibraries/Biohazrd/issues/172")]
        public void OutOfScopeIgnoredDeclarationInsideInScopeDeclarationIsNotInScope()
        {
            TranslatedLibraryBuilder builder = new();
            builder.AddFile(new SourceFile("A.h")
            {
                Contents = "struct __declspec(dllexport) MyStruct;",
                IsInScope = false,
                IndexDirectly = false
            });

            builder.AddFile(new SourceFile("B.h")
            {
                Contents = @"
#include ""A.h""

struct MyStruct
{
    int x;
};
"
            });

            TranslatedLibrary library = builder.Create();

            Assert.Empty(library.ParsingDiagnostics);
            Assert.Single(library.Files);
            Assert.Single(library.Declarations);
            Assert.Equal("MyStruct", library.Declarations[0].Name);
        }