cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

AnalyserModel returned from Analyser gets broken when the Analyser goes out of scope #1130

Closed nickerso closed 1 year ago

nickerso commented 1 year ago

After some crafty debugging from @hsorby, he found that the underlying cause of an issue I have been seeing is that the analyser model returned from an analyser we lose some critical information, needed by the generator, once the originating analyser goes out of scope (see test below). This is not typically an issue in our current testing as analysing and code generation tends to happen in a single chunk of code. The following test will fail when retrieving the generator->implementationCode() string when the GeneratorImpl::generateCode() method attempts to navigate the AST.

TEST(Generator, analyserModelScopeTest)
{
    libcellml::ParserPtr parser = libcellml::Parser::create(false);

    auto model = parser->parseModel(fileContents("model.cellml"));

    libcellml::AnalyserModelPtr analyserModel = nullptr;
    {
        libcellml::AnalyserPtr analyser = libcellml::Analyser::create();
        analyser->analyseModel(model);
        EXPECT_EQ(size_t(0), analyser->errorCount());
        analyserModel = analyser->model();
    }

    auto generator = libcellml::Generator::create();

    generator->setModel(analyserModel);

    EXPECT_EQ(EMPTY_STRING, generator->interfaceCode());
    EXPECT_EQ(EMPTY_STRING, generator->implementationCode());
}