CppEditor: Make a few CppModelManager functions static

... to simplify code using it.

Change-Id: Ib2bfbb5a17a1d48088a9cf4973d4c3f006cd34e4
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2023-07-12 09:47:29 +02:00
parent 8f82e76ba1
commit 0b83f110f4
80 changed files with 505 additions and 619 deletions

View File

@@ -392,7 +392,7 @@ void AutotestPluginPrivate::onRunUnderCursorTriggered(TestRunMode mode)
const int line = currentEditor->currentLine();
const FilePath filePath = currentEditor->textDocument()->filePath();
const CPlusPlus::Snapshot snapshot = CppEditor::CppModelManager::instance()->snapshot();
const CPlusPlus::Snapshot snapshot = CppEditor::CppModelManager::snapshot();
const CPlusPlus::Document::Ptr doc = snapshot.document(filePath);
if (doc.isNull()) // not part of C++ snapshot
return;

View File

@@ -104,9 +104,8 @@ bool BoostTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
if (doc.isNull() || !includesBoostTest(doc, m_cppSnapshot) || !hasBoostTestMacros(doc))
return false;
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
const QList<CppEditor::ProjectPart::ConstPtr> projectParts
= modelManager->projectPart(fileName);
= CppEditor::CppModelManager::projectPart(fileName);
if (projectParts.isEmpty()) // happens if shutting down while parsing
return false;
const CppEditor::ProjectPart::ConstPtr projectPart = projectParts.first();

View File

@@ -178,10 +178,9 @@ QList<ITestConfiguration *> BoostTestTreeItem::getAllTestConfigurations() const
++funcChildren;
});
if (funcChildren) {
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return);
testsPerProjectfile[item->proFile()].testCases += funcChildren;
testsPerProjectfile[item->proFile()].internalTargets.unite(cppMM->internalTargets(item->filePath()));
testsPerProjectfile[item->proFile()].internalTargets.unite(
CppEditor::CppModelManager::internalTargets(item->filePath()));
}
});
@@ -219,8 +218,6 @@ QList<ITestConfiguration *> BoostTestTreeItem::getTestConfigurations(
if (!item->enabled()) // ignore child tests known to be disabled when using run selected
return;
if (predicate(item)) {
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return);
QString tcName = item->name();
if (item->state().testFlag(BoostTestTreeItem::Templated))
tcName.append("<*");
@@ -229,7 +226,8 @@ QList<ITestConfiguration *> BoostTestTreeItem::getTestConfigurations(
tcName = handleSpecialFunctionNames(tcName);
testCasesForProjectFile[item->proFile()].testCases.append(
item->prependWithParentsSuitePaths(tcName));
testCasesForProjectFile[item->proFile()].internalTargets.unite(cppMM->internalTargets(item->filePath()));
testCasesForProjectFile[item->proFile()].internalTargets.unite(
CppEditor::CppModelManager::internalTargets(item->filePath()));
}
});
@@ -265,8 +263,6 @@ ITestConfiguration *BoostTestTreeItem::testConfiguration() const
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
QTC_ASSERT(project, return nullptr);
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return nullptr);
const Type itemType = type();
if (itemType == TestSuite || itemType == TestCase) {
@@ -300,7 +296,7 @@ ITestConfiguration *BoostTestTreeItem::testConfiguration() const
config->setProjectFile(proFile());
config->setProject(project);
config->setTestCases(testCases);
config->setInternalTargets(cppMM->internalTargets(filePath()));
config->setInternalTargets(CppEditor::CppModelManager::internalTargets(filePath()));
return config;
}
return nullptr;

View File

@@ -99,7 +99,6 @@ bool CatchTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
if (doc.isNull() || !includesCatchHeader(doc, m_cppSnapshot))
return false;
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
const QString &filePath = doc->filePath().toString();
const QByteArray &fileContent = getFileContent(fileName);
@@ -115,7 +114,8 @@ bool CatchTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
}
const QList<CppEditor::ProjectPart::ConstPtr> projectParts = modelManager->projectPart(fileName);
const QList<CppEditor::ProjectPart::ConstPtr> projectParts
= CppEditor::CppModelManager::projectPart(fileName);
if (projectParts.isEmpty()) // happens if shutting down while parsing
return false;
FilePath proFile;

View File

@@ -145,8 +145,6 @@ ITestConfiguration *CatchTreeItem::testConfiguration() const
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
QTC_ASSERT(project, return nullptr);
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return nullptr);
if (type() != TestCase)
return nullptr;
@@ -157,7 +155,7 @@ ITestConfiguration *CatchTreeItem::testConfiguration() const
config->setProjectFile(proFile());
config->setProject(project);
config->setTestCases(QStringList(testCasesString()));
config->setInternalTargets(cppMM->internalTargets(filePath()));
config->setInternalTargets(CppEditor::CppModelManager::internalTargets(filePath()));
return config;
}
@@ -180,8 +178,6 @@ static void collectTestInfo(const TestTreeItem *item,
bool ignoreCheckState)
{
QTC_ASSERT(item, return);
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return);
const int childCount = item->childCount();
if (item->type() == TestTreeItem::GroupNode) {
item->forFirstLevelChildItems([&testCasesForProfile, ignoreCheckState](TestTreeItem *it) {
@@ -198,15 +194,16 @@ static void collectTestInfo(const TestTreeItem *item,
CatchTreeItem *current = static_cast<CatchTreeItem *>(it);
testCasesForProfile[projectFile].names.append(current->testCasesString());
});
testCasesForProfile[projectFile].internalTargets.unite(cppMM->internalTargets(item->filePath()));
testCasesForProfile[projectFile].internalTargets.unite(
CppEditor::CppModelManager::internalTargets(item->filePath()));
} else if (item->checked() == Qt::PartiallyChecked) {
item->forFirstLevelChildItems([&testCasesForProfile, cppMM](TestTreeItem *child) {
item->forFirstLevelChildItems([&testCasesForProfile](TestTreeItem *child) {
QTC_ASSERT(child->type() == TestTreeItem::TestCase, return);
if (child->checked() == Qt::Checked) {
CatchTreeItem *current = static_cast<CatchTreeItem *>(child);
testCasesForProfile[child->proFile()].names.append(current->testCasesString());
testCasesForProfile[child->proFile()].internalTargets.unite(
cppMM->internalTargets(child->filePath()));
CppEditor::CppModelManager::internalTargets(child->filePath()));
}
});
@@ -222,13 +219,11 @@ static void collectFailedTestInfo(const CatchTreeItem *item,
item->forAllChildItems([&testCasesForProfile](TestTreeItem *it) {
QTC_ASSERT(it, return);
QTC_ASSERT(it->parentItem(), return);
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return);
if (it->type() == TestTreeItem::TestCase && it->data(0, FailedRole).toBool()) {
CatchTreeItem *current = static_cast<CatchTreeItem *>(it);
testCasesForProfile[it->proFile()].names.append(current->testCasesString());
testCasesForProfile[it->proFile()].internalTargets.unite(
cppMM->internalTargets(it->filePath()));
CppEditor::CppModelManager::internalTargets(it->filePath()));
}
});
}
@@ -270,8 +265,6 @@ QList<ITestConfiguration *> CatchTreeItem::getFailedTestConfigurations() const
QList<ITestConfiguration *> CatchTreeItem::getTestConfigurationsForFile(const FilePath &fileName) const
{
QList<ITestConfiguration *> result;
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return result);
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
if (!project || type() != Root)
@@ -296,7 +289,8 @@ QList<ITestConfiguration *> CatchTreeItem::getTestConfigurationsForFile(const Fi
testConfig->setTestCases(testCases);
testConfig->setProjectFile(item->proFile());
testConfig->setProject(ProjectExplorer::ProjectManager::startupProject());
testConfig->setInternalTargets(cppMM->internalTargets(item->filePath()));
testConfig->setInternalTargets(
CppEditor::CppModelManager::internalTargets(item->filePath()));
result << testConfig;
}

View File

@@ -86,7 +86,6 @@ bool GTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
}
const FilePath filePath = doc->filePath();
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
CPlusPlus::Document::Ptr document = m_cppSnapshot.preprocessedDocument(fileContent, fileName);
document->check();
CPlusPlus::AST *ast = document->translationUnit()->ast();
@@ -95,7 +94,8 @@ bool GTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
const QMap<GTestCaseSpec, GTestCodeLocationList> result = visitor.gtestFunctions();
FilePath proFile;
const QList<CppEditor::ProjectPart::ConstPtr> &ppList = modelManager->projectPart(filePath);
const QList<CppEditor::ProjectPart::ConstPtr> &ppList =
CppEditor::CppModelManager::projectPart(filePath);
if (!ppList.isEmpty())
proFile = FilePath::fromString(ppList.first()->projectFile);
else

View File

@@ -504,14 +504,14 @@ QString GTestTreeItem::nameSuffix() const
QSet<QString> internalTargets(const TestTreeItem &item)
{
QSet<QString> result;
const auto cppMM = CppEditor::CppModelManager::instance();
const auto projectInfo = cppMM->projectInfo(ProjectExplorer::ProjectManager::startupProject());
const auto projectInfo = CppEditor::CppModelManager::projectInfo(
ProjectExplorer::ProjectManager::startupProject());
if (!projectInfo)
return {};
const FilePath filePath = item.filePath();
const QVector<CppEditor::ProjectPart::ConstPtr> projectParts = projectInfo->projectParts();
if (projectParts.isEmpty())
return cppMM->dependingInternalTargets(item.filePath());
return CppEditor::CppModelManager::dependingInternalTargets(item.filePath());
for (const CppEditor::ProjectPart::ConstPtr &projectPart : projectParts) {
if (FilePath::fromString(projectPart->projectFile) == item.proFile()
&& Utils::anyOf(projectPart->files, [&filePath](const CppEditor::ProjectFile &pf) {
@@ -519,7 +519,7 @@ QSet<QString> internalTargets(const TestTreeItem &item)
})) {
result.insert(projectPart->buildSystemTarget);
if (projectPart->buildTargetType != ProjectExplorer::BuildTargetType::Executable)
result.unite(cppMM->dependingInternalTargets(filePath));
result.unite(CppEditor::CppModelManager::dependingInternalTargets(filePath));
}
}
return result;

View File

@@ -28,14 +28,14 @@ void CppParser::init(const QSet<FilePath> &filesToParse, bool fullParse)
{
Q_UNUSED(filesToParse)
Q_UNUSED(fullParse)
m_cppSnapshot = CppEditor::CppModelManager::instance()->snapshot();
m_workingCopy = CppEditor::CppModelManager::instance()->workingCopy();
m_cppSnapshot = CppEditor::CppModelManager::snapshot();
m_workingCopy = CppEditor::CppModelManager::workingCopy();
}
bool CppParser::selectedForBuilding(const FilePath &fileName)
{
QList<CppEditor::ProjectPart::ConstPtr> projParts =
CppEditor::CppModelManager::instance()->projectPart(fileName);
CppEditor::CppModelManager::projectPart(fileName);
return !projParts.isEmpty() && projParts.at(0)->selectedForBuilding;
}
@@ -62,8 +62,8 @@ bool precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const QString &cacheString,
const std::function<bool(const FilePath &)> &checker)
{
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
const QList<CppEditor::ProjectPart::ConstPtr> projectParts = modelManager->projectPart(filePath);
const QList<CppEditor::ProjectPart::ConstPtr> projectParts
= CppEditor::CppModelManager::projectPart(filePath);
if (projectParts.isEmpty())
return false;
const QStringList precompiledHeaders = projectParts.first()->precompiledHeaders;

View File

@@ -74,7 +74,7 @@ static bool includesQtTest(const CPlusPlus::Document::Ptr &doc, const CPlusPlus:
static bool qtTestLibDefined(const FilePath &fileName)
{
const QList<CppEditor::ProjectPart::ConstPtr> parts =
CppEditor::CppModelManager::instance()->projectPart(fileName);
CppEditor::CppModelManager::projectPart(fileName);
if (parts.size() > 0) {
return Utils::anyOf(parts.at(0)->projectMacros, [](const ProjectExplorer::Macro &macro) {
return macro.key == "QT_TESTLIB_LIB";
@@ -83,11 +83,10 @@ static bool qtTestLibDefined(const FilePath &fileName)
return false;
}
TestCases QtTestParser::testCases(const CppEditor::CppModelManager *modelManager,
const FilePath &filePath) const
TestCases QtTestParser::testCases(const FilePath &filePath) const
{
const QByteArray &fileContent = getFileContent(filePath);
CPlusPlus::Document::Ptr document = modelManager->document(filePath);
CPlusPlus::Document::Ptr document = CppEditor::CppModelManager::document(filePath);
if (document.isNull())
return {};
@@ -305,8 +304,7 @@ bool QtTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
return false;
}
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
TestCases testCaseList(testCases(modelManager, fileName));
TestCases testCaseList(testCases(fileName));
bool reported = false;
// we might be in a reparse without the original entry point with the QTest::qExec()
if (testCaseList.isEmpty() && !oldTestCases.empty())
@@ -319,7 +317,7 @@ bool QtTestParser::processDocument(QPromise<TestParseResultPtr> &promise,
continue;
QList<CppEditor::ProjectPart::ConstPtr> projectParts
= modelManager->projectPart(fileName);
= CppEditor::CppModelManager::projectPart(fileName);
if (projectParts.isEmpty()) // happens if shutting down while parsing
return false;

View File

@@ -10,8 +10,6 @@
#include <optional>
namespace CppEditor { class CppModelManager; }
namespace Autotest {
namespace Internal {
@@ -40,8 +38,7 @@ public:
const Utils::FilePath &fileName) override;
private:
TestCases testCases(const CppEditor::CppModelManager *modelManager,
const Utils::FilePath &fileName) const;
TestCases testCases(const Utils::FilePath &fileName) const;
QHash<QString, QtTestCodeLocationList> checkForDataTags(const Utils::FilePath &fileName) const;
struct TestCaseData {
Utils::FilePath fileName;

View File

@@ -120,8 +120,6 @@ ITestConfiguration *QtTestTreeItem::testConfiguration() const
{
ProjectExplorer::Project *project = ProjectExplorer::ProjectManager::startupProject();
QTC_ASSERT(project, return nullptr);
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return nullptr);
QtTestConfiguration *config = nullptr;
switch (type()) {
@@ -155,15 +153,13 @@ ITestConfiguration *QtTestTreeItem::testConfiguration() const
return nullptr;
}
if (config)
config->setInternalTargets(cppMM->internalTargets(filePath()));
config->setInternalTargets(CppEditor::CppModelManager::internalTargets(filePath()));
return config;
}
static void fillTestConfigurationsFromCheckState(const TestTreeItem *item,
QList<ITestConfiguration *> &testConfigurations)
{
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return);
QTC_ASSERT(item, return);
if (item->type() == TestTreeItem::GroupNode) {
for (int row = 0, count = item->childCount(); row < count; ++row)
@@ -198,15 +194,14 @@ static void fillTestConfigurationsFromCheckState(const TestTreeItem *item,
testConfig->setTestCases(testCases);
testConfig->setProjectFile(item->proFile());
testConfig->setProject(ProjectExplorer::ProjectManager::startupProject());
testConfig->setInternalTargets(cppMM->internalTargets(item->filePath()));
testConfig->setInternalTargets(
CppEditor::CppModelManager::internalTargets(item->filePath()));
testConfigurations << testConfig;
}
}
static void collectFailedTestInfo(TestTreeItem *item, QList<ITestConfiguration *> &testConfigs)
{
const auto cppMM = CppEditor::CppModelManager::instance();
QTC_ASSERT(cppMM, return);
QTC_ASSERT(item, return);
if (item->type() == TestTreeItem::GroupNode) {
for (int row = 0, count = item->childCount(); row < count; ++row)
@@ -232,7 +227,8 @@ static void collectFailedTestInfo(TestTreeItem *item, QList<ITestConfiguration *
testConfig->setTestCases(testCases);
testConfig->setProjectFile(item->proFile());
testConfig->setProject(ProjectExplorer::ProjectManager::startupProject());
testConfig->setInternalTargets(cppMM->internalTargets(item->filePath()));
testConfig->setInternalTargets(
CppEditor::CppModelManager::internalTargets(item->filePath()));
testConfigs << testConfig;
}

View File

@@ -82,9 +82,10 @@ static bool includesQtQuickTest(const CPlusPlus::Document::Ptr &doc,
return false;
}
static QString quickTestSrcDir(const CppEditor::CppModelManager *cppMM, const FilePath &fileName)
static QString quickTestSrcDir(const FilePath &fileName)
{
const QList<CppEditor::ProjectPart::ConstPtr> parts = cppMM->projectPart(fileName);
const QList<CppEditor::ProjectPart::ConstPtr> parts =
CppEditor::CppModelManager::projectPart(fileName);
if (parts.size() > 0) {
const ProjectExplorer::Macros &macros = parts.at(0)->projectMacros;
auto found = std::find_if(macros.cbegin(), macros.cend(),
@@ -252,17 +253,17 @@ bool QuickTestParser::handleQtQuickTest(QPromise<TestParseResultPtr> &promise,
CPlusPlus::Document::Ptr document,
ITestFramework *framework)
{
const CppEditor::CppModelManager *modelManager = CppEditor::CppModelManager::instance();
if (quickTestName(document).isEmpty())
return false;
QList<CppEditor::ProjectPart::ConstPtr> ppList = modelManager->projectPart(document->filePath());
QList<CppEditor::ProjectPart::ConstPtr> ppList =
CppEditor::CppModelManager::projectPart(document->filePath());
if (ppList.isEmpty()) // happens if shutting down while parsing
return false;
const FilePath cppFileName = document->filePath();
const FilePath proFile = FilePath::fromString(ppList.at(0)->projectFile);
m_mainCppFiles.insert(cppFileName, proFile);
const FilePath srcDir = FilePath::fromString(quickTestSrcDir(modelManager, cppFileName));
const FilePath srcDir = FilePath::fromString(quickTestSrcDir(cppFileName));
if (srcDir.isEmpty())
return false;

View File

@@ -370,8 +370,8 @@ bool QuickTestTreeItem::isGroupable() const
QSet<QString> internalTargets(const FilePath &proFile)
{
QSet<QString> result;
const auto cppMM = CppEditor::CppModelManager::instance();
const auto projectInfo = cppMM->projectInfo(ProjectExplorer::ProjectManager::startupProject());
const auto projectInfo =
CppEditor::CppModelManager::projectInfo(ProjectExplorer::ProjectManager::startupProject());
if (!projectInfo)
return {};
for (const CppEditor::ProjectPart::ConstPtr &projectPart : projectInfo->projectParts()) {

View File

@@ -337,7 +337,7 @@ void TestCodeParser::scanForTests(const QSet<FilePath> &filePaths,
qCDebug(LOG) << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") << "StartParsing";
m_parsingTimer.restart();
QSet<QString> extensions;
const auto cppSnapshot = CppEditor::CppModelManager::instance()->snapshot();
const auto cppSnapshot = CppEditor::CppModelManager::snapshot();
for (ITestParser *parser : codeParsers) {
parser->init(files, isFullParse);