diff options
author | Christian Kandeler <christian.kandeler@qt.io> | 2023-05-02 17:55:44 +0200 |
---|---|---|
committer | Christian Kandeler <christian.kandeler@qt.io> | 2023-05-03 09:50:53 +0000 |
commit | a03bea81c7bb994367855ab40b9d76e974bbff50 (patch) | |
tree | 2a8484d47a6d1f148855971086df91a7366b20c5 | |
parent | e15b38494433a6f3e608f561517966ea0d745ddf (diff) |
CppEditor: Remove WorkingCopy::contains()
Most uses involved a double look-up.
Change-Id: Ifeb62ea2361222ed0faad749f44a59735c8d6930
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
-rw-r--r-- | src/plugins/autotest/itestparser.cpp | 4 | ||||
-rw-r--r-- | src/plugins/cppeditor/builtineditordocumentparser.cpp | 4 | ||||
-rw-r--r-- | src/plugins/cppeditor/builtineditordocumentprocessor.cpp | 13 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppfindreferences.cpp | 4 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppmodelmanager_test.cpp | 10 | ||||
-rw-r--r-- | src/plugins/cppeditor/cpprefactoringchanges.cpp | 4 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppsourceprocessor.cpp | 18 | ||||
-rw-r--r-- | src/plugins/cppeditor/cpptoolsjsextension.cpp | 6 | ||||
-rw-r--r-- | src/plugins/cppeditor/cpptoolstestcase.cpp | 2 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppworkingcopy.cpp | 14 | ||||
-rw-r--r-- | src/plugins/cppeditor/cppworkingcopy.h | 11 | ||||
-rw-r--r-- | src/plugins/cppeditor/fileandtokenactions_test.cpp | 2 | ||||
-rw-r--r-- | src/plugins/debugger/cdb/cdbengine.cpp | 4 | ||||
-rw-r--r-- | src/plugins/designer/qtcreatorintegration.cpp | 4 |
14 files changed, 56 insertions, 44 deletions
diff --git a/src/plugins/autotest/itestparser.cpp b/src/plugins/autotest/itestparser.cpp index 27408ee933..79fde2a0bb 100644 --- a/src/plugins/autotest/itestparser.cpp +++ b/src/plugins/autotest/itestparser.cpp @@ -43,8 +43,8 @@ bool CppParser::selectedForBuilding(const FilePath &fileName) QByteArray CppParser::getFileContent(const FilePath &filePath) const { QByteArray fileContent; - if (m_workingCopy.contains(filePath)) { - fileContent = m_workingCopy.source(filePath); + if (const auto source = m_workingCopy.source(filePath)) { + fileContent = *source; } else { QString error; const QTextCodec *codec = Core::EditorManager::defaultTextCodec(); diff --git a/src/plugins/cppeditor/builtineditordocumentparser.cpp b/src/plugins/cppeditor/builtineditordocumentparser.cpp index ca7b83960d..f6d7d6faf2 100644 --- a/src/plugins/cppeditor/builtineditordocumentparser.cpp +++ b/src/plugins/cppeditor/builtineditordocumentparser.cpp @@ -144,8 +144,8 @@ void BuiltinEditorDocumentParser::updateImpl(const QPromise<void> &promise, QSet<Utils::FilePath> toRemove; for (const Document::Ptr &doc : std::as_const(state.snapshot)) { const Utils::FilePath filePath = doc->filePath(); - if (workingCopy.contains(filePath)) { - if (workingCopy.get(filePath).second != doc->editorRevision()) + if (const auto entry = workingCopy.get(filePath)) { + if (entry->second != doc->editorRevision()) addFileAndDependencies(&state.snapshot, &toRemove, filePath); continue; } diff --git a/src/plugins/cppeditor/builtineditordocumentprocessor.cpp b/src/plugins/cppeditor/builtineditordocumentprocessor.cpp index 0ea0c9c2dd..625bed9c1b 100644 --- a/src/plugins/cppeditor/builtineditordocumentprocessor.cpp +++ b/src/plugins/cppeditor/builtineditordocumentprocessor.cpp @@ -302,12 +302,13 @@ void BuiltinEditorDocumentProcessor::onCodeWarningsUpdated( SemanticInfo::Source BuiltinEditorDocumentProcessor::createSemanticInfoSource(bool force) const { - const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy(); - return SemanticInfo::Source(filePath().toString(), - workingCopy.source(filePath()), - workingCopy.revision(filePath()), - m_documentSnapshot, - force); + QByteArray source; + int revision = 0; + if (const auto entry = CppModelManager::instance()->workingCopy().get(filePath())) { + source = entry->first; + revision = entry->second; + } + return SemanticInfo::Source(filePath().toString(), source, revision, m_documentSnapshot, force); } } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppfindreferences.cpp b/src/plugins/cppeditor/cppfindreferences.cpp index 589ac2d1a6..a4a3aba507 100644 --- a/src/plugins/cppeditor/cppfindreferences.cpp +++ b/src/plugins/cppeditor/cppfindreferences.cpp @@ -105,8 +105,8 @@ namespace Internal { static QByteArray getSource(const Utils::FilePath &fileName, const WorkingCopy &workingCopy) { - if (workingCopy.contains(fileName)) { - return workingCopy.source(fileName); + if (const auto source = workingCopy.source(fileName)) { + return *source; } else { QString fileContents; Utils::TextFileFormat format; diff --git a/src/plugins/cppeditor/cppmodelmanager_test.cpp b/src/plugins/cppeditor/cppmodelmanager_test.cpp index eaa2ef142d..c3fb341646 100644 --- a/src/plugins/cppeditor/cppmodelmanager_test.cpp +++ b/src/plugins/cppeditor/cppmodelmanager_test.cpp @@ -649,7 +649,7 @@ void ModelManagerTest::testGcIfLastCppeditorClosed() QVERIFY(editor); QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1); QVERIFY(mm->isCppEditor(editor)); - QVERIFY(mm->workingCopy().contains(file)); + QVERIFY(mm->workingCopy().get(file)); // Wait until the file is refreshed helper.waitForRefreshedSourceFiles(); @@ -659,7 +659,7 @@ void ModelManagerTest::testGcIfLastCppeditorClosed() helper.waitForFinishedGc(); // Check: File is removed from the snapshpt - QVERIFY(!mm->workingCopy().contains(file)); + QVERIFY(!mm->workingCopy().get(file)); QVERIFY(!mm->snapshot().contains(file)); } @@ -684,13 +684,13 @@ void ModelManagerTest::testDontGcOpenedFiles() // Wait until the file is refreshed and check whether it is in the working copy helper.waitForRefreshedSourceFiles(); - QVERIFY(mm->workingCopy().contains(file)); + QVERIFY(mm->workingCopy().get(file)); // Run the garbage collector mm->GC(); // Check: File is still there - QVERIFY(mm->workingCopy().contains(file)); + QVERIFY(mm->workingCopy().get(file)); QVERIFY(mm->snapshot().contains(file)); // Close editor @@ -1090,7 +1090,7 @@ void ModelManagerTest::testRenameIncludesInEditor() }); QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1); QVERIFY(modelManager->isCppEditor(editor)); - QVERIFY(modelManager->workingCopy().contains(mainFile)); + QVERIFY(modelManager->workingCopy().get(mainFile)); // Test the renaming of a header file where a pragma once guard is present QVERIFY(Core::FileUtils::renameFile(headerWithPragmaOnce, diff --git a/src/plugins/cppeditor/cpprefactoringchanges.cpp b/src/plugins/cppeditor/cpprefactoringchanges.cpp index 1cce747fc6..bb34174d06 100644 --- a/src/plugins/cppeditor/cpprefactoringchanges.cpp +++ b/src/plugins/cppeditor/cpprefactoringchanges.cpp @@ -57,8 +57,8 @@ CppRefactoringFilePtr CppRefactoringChanges::file(const FilePath &filePath) cons CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const FilePath &filePath) const { QTextDocument *document = nullptr; - if (data()->m_workingCopy.contains(filePath)) - document = new QTextDocument(QString::fromUtf8(data()->m_workingCopy.source(filePath))); + if (const auto source = data()->m_workingCopy.source(filePath)) + document = new QTextDocument(QString::fromUtf8(*source)); CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath)); result->m_data = m_data; diff --git a/src/plugins/cppeditor/cppsourceprocessor.cpp b/src/plugins/cppeditor/cppsourceprocessor.cpp index 66cca58a0f..55dc1f10e0 100644 --- a/src/plugins/cppeditor/cppsourceprocessor.cpp +++ b/src/plugins/cppeditor/cppsourceprocessor.cpp @@ -81,7 +81,8 @@ inline const CPlusPlus::Macro revision(const WorkingCopy &workingCopy, const CPlusPlus::Macro ¯o) { CPlusPlus::Macro newMacro(macro); - newMacro.setFileRevision(workingCopy.get(macro.filePath()).second); + if (const auto entry = workingCopy.get(macro.filePath())) + newMacro.setFileRevision(entry->second); return newMacro; } @@ -189,10 +190,9 @@ bool CppSourceProcessor::getFileContents(const FilePath &absoluteFilePath, return false; // Get from working copy - if (m_workingCopy.contains(absoluteFilePath)) { - const QPair<QByteArray, unsigned> entry = m_workingCopy.get(absoluteFilePath); - *contents = entry.first; - *revision = entry.second; + if (const auto entry = m_workingCopy.get(absoluteFilePath)) { + *contents = entry->first; + *revision = entry->second; return true; } @@ -216,7 +216,7 @@ bool CppSourceProcessor::checkFile(const FilePath &absoluteFilePath) const { if (absoluteFilePath.isEmpty() || m_included.contains(absoluteFilePath) - || m_workingCopy.contains(absoluteFilePath)) { + || m_workingCopy.get(absoluteFilePath)) { return true; } @@ -281,7 +281,7 @@ FilePath CppSourceProcessor::resolveFile_helper(const FilePath &filePath, } else { path = FilePath::fromString(headerPathsIt->path) / fileName; } - if (m_workingCopy.contains(path) || checkFile(path)) + if (m_workingCopy.get(path) || checkFile(path)) return path; } } @@ -468,8 +468,8 @@ void CppSourceProcessor::sourceNeeded(int line, const FilePath &filePath, Includ document->setUtf8Source(preprocessedCode); document->keepSourceAndAST(); document->tokenize(); - document->check(m_workingCopy.contains(document->filePath()) ? Document::FullCheck - : Document::FastCheck); + document->check(m_workingCopy.get(document->filePath()) ? Document::FullCheck + : Document::FastCheck); m_documentFinished(document); diff --git a/src/plugins/cppeditor/cpptoolsjsextension.cpp b/src/plugins/cppeditor/cpptoolsjsextension.cpp index b70328601e..73a846593c 100644 --- a/src/plugins/cppeditor/cpptoolsjsextension.cpp +++ b/src/plugins/cppeditor/cpptoolsjsextension.cpp @@ -135,13 +135,13 @@ bool CppToolsJsExtension::hasQObjectParent(const QString &klassName) const // Find class in AST. const CPlusPlus::Snapshot snapshot = CppModelManager::instance()->snapshot(); const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy(); - QByteArray source = workingCopy.source(item->filePath()); - if (source.isEmpty()) { + std::optional<QByteArray> source = workingCopy.source(item->filePath()); + if (!source) { const Utils::expected_str<QByteArray> contents = item->filePath().fileContents(); QTC_ASSERT_EXPECTED(contents, return false); source = *contents; } - const auto doc = snapshot.preprocessedDocument(source, item->filePath()); + const auto doc = snapshot.preprocessedDocument(*source, item->filePath()); if (!doc) return false; doc->check(); diff --git a/src/plugins/cppeditor/cpptoolstestcase.cpp b/src/plugins/cppeditor/cpptoolstestcase.cpp index 2d149b3ff5..706423c051 100644 --- a/src/plugins/cppeditor/cpptoolstestcase.cpp +++ b/src/plugins/cppeditor/cpptoolstestcase.cpp @@ -158,7 +158,7 @@ bool VerifyCleanCppModelManager::isClean(bool testOnlyForCleanedProjects) if (!testOnlyForCleanedProjects) { RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty()); RETURN_FALSE_IF_NOT(mm->workingCopy().size() == 1); - RETURN_FALSE_IF_NOT(mm->workingCopy().contains(mm->configurationFileName())); + RETURN_FALSE_IF_NOT(mm->workingCopy().get(mm->configurationFileName())); } return true; } diff --git a/src/plugins/cppeditor/cppworkingcopy.cpp b/src/plugins/cppeditor/cppworkingcopy.cpp index b8922b7076..828388c96c 100644 --- a/src/plugins/cppeditor/cppworkingcopy.cpp +++ b/src/plugins/cppeditor/cppworkingcopy.cpp @@ -20,4 +20,18 @@ namespace CppEditor { WorkingCopy::WorkingCopy() = default; +std::optional<QByteArray> WorkingCopy::source(const Utils::FilePath &fileName) const +{ + if (const auto value = get(fileName)) + return value->first; + return {}; +} + +std::optional<QPair<QByteArray, unsigned>> WorkingCopy::get(const Utils::FilePath &fileName) const +{ + const auto it = _elements.constFind(fileName); + if (it == _elements.constEnd()) + return {}; + return it.value(); +} } // namespace CppEditor diff --git a/src/plugins/cppeditor/cppworkingcopy.h b/src/plugins/cppeditor/cppworkingcopy.h index ccc8258745..87a613bc7a 100644 --- a/src/plugins/cppeditor/cppworkingcopy.h +++ b/src/plugins/cppeditor/cppworkingcopy.h @@ -11,6 +11,8 @@ #include <QString> #include <QPair> +#include <optional> + namespace CppEditor { class CPPEDITOR_EXPORT WorkingCopy @@ -21,17 +23,12 @@ public: void insert(const Utils::FilePath &fileName, const QByteArray &source, unsigned revision = 0) { _elements.insert(fileName, {source, revision}); } - bool contains(const Utils::FilePath &fileName) const - { return _elements.contains(fileName); } - - QByteArray source(const Utils::FilePath &fileName) const - { return _elements.value(fileName).first; } + std::optional<QByteArray> source(const Utils::FilePath &fileName) const; unsigned revision(const Utils::FilePath &fileName) const { return _elements.value(fileName).second; } - QPair<QByteArray, unsigned> get(const Utils::FilePath &fileName) const - { return _elements.value(fileName); } + std::optional<QPair<QByteArray, unsigned>> get(const Utils::FilePath &fileName) const; using Table = QHash<Utils::FilePath, QPair<QByteArray, unsigned> >; const Table &elements() const diff --git a/src/plugins/cppeditor/fileandtokenactions_test.cpp b/src/plugins/cppeditor/fileandtokenactions_test.cpp index b4b60d6828..2ad6b6b4ef 100644 --- a/src/plugins/cppeditor/fileandtokenactions_test.cpp +++ b/src/plugins/cppeditor/fileandtokenactions_test.cpp @@ -163,7 +163,7 @@ TestActionsTestCase::TestActionsTestCase(const Actions &tokenActions, const Acti QCOMPARE(DocumentModel::openedDocuments().size(), 1); QVERIFY(m_modelManager->isCppEditor(editor)); - QVERIFY(m_modelManager->workingCopy().contains(filePath)); + QVERIFY(m_modelManager->workingCopy().get(filePath)); // Rehighlight waitForRehighlightedSemanticDocument(editorWidget); diff --git a/src/plugins/debugger/cdb/cdbengine.cpp b/src/plugins/debugger/cdb/cdbengine.cpp index 0f316faa76..5d5b828712 100644 --- a/src/plugins/debugger/cdb/cdbengine.cpp +++ b/src/plugins/debugger/cdb/cdbengine.cpp @@ -2466,8 +2466,8 @@ static CPlusPlus::Document::Ptr getParsedDocument(const FilePath &filePath, const CPlusPlus::Snapshot &snapshot) { QByteArray src; - if (workingCopy.contains(filePath)) - src = workingCopy.source(filePath); + if (const auto source = workingCopy.source(filePath)) + src = *source; else src = QString::fromLocal8Bit(filePath.fileContents().value_or(QByteArray())).toUtf8(); diff --git a/src/plugins/designer/qtcreatorintegration.cpp b/src/plugins/designer/qtcreatorintegration.cpp index 02f01a5d09..60189282ce 100644 --- a/src/plugins/designer/qtcreatorintegration.cpp +++ b/src/plugins/designer/qtcreatorintegration.cpp @@ -456,8 +456,8 @@ static Document::Ptr getParsedDocument(const FilePath &filePath, Snapshot &snapshot) { QByteArray src; - if (workingCopy.contains(filePath)) { - src = workingCopy.source(filePath); + if (const auto source = workingCopy.source(filePath)) { + src = *source; } else { Utils::FileReader reader; if (reader.fetch(filePath)) // ### FIXME error reporting |