Biohazrd icon indicating copy to clipboard operation
Biohazrd copied to clipboard

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

Open PathogenDavid opened this issue 3 years ago • 1 comments

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 avatar Feb 27 '21 20:02 PathogenDavid

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);
        }

PathogenDavid avatar Feb 27 '21 20:02 PathogenDavid