diff --git a/src/tools/clangbackend/ipcsource/translationunits.cpp b/src/tools/clangbackend/ipcsource/translationunits.cpp index 5ec8be8810c..713c4e59755 100644 --- a/src/tools/clangbackend/ipcsource/translationunits.cpp +++ b/src/tools/clangbackend/ipcsource/translationunits.cpp @@ -282,9 +282,10 @@ TranslationUnit TranslationUnits::createTranslationUnit(const FileContainer &fil void TranslationUnits::updateTranslationUnit(const FileContainer &fileContainer) { - auto findIterator = findAllTranslationUnitWithFilePath(fileContainer.filePath()); - if (findIterator != translationUnits_.end()) - findIterator->setDocumentRevision(fileContainer.documentRevision()); + const auto translationUnits = findAllTranslationUnitWithFilePath(fileContainer.filePath()); + + for (auto translationUnit : translationUnits) + translationUnit.setDocumentRevision(fileContainer.documentRevision()); } std::vector::iterator TranslationUnits::findTranslationUnit(const FileContainer &fileContainer) @@ -292,13 +293,19 @@ std::vector::iterator TranslationUnits::findTranslationUnit(con return std::find(translationUnits_.begin(), translationUnits_.end(), fileContainer); } -std::vector::iterator TranslationUnits::findAllTranslationUnitWithFilePath(const Utf8String &filePath) +std::vector TranslationUnits::findAllTranslationUnitWithFilePath(const Utf8String &filePath) { - auto filePathCompare = [&filePath] (const TranslationUnit &translationUnit) { + const auto filePathCompare = [&filePath] (const TranslationUnit &translationUnit) { return translationUnit.filePath() == filePath; }; - return std::find_if(translationUnits_.begin(), translationUnits_.end(), filePathCompare); + std::vector translationUnits; + std::copy_if(translationUnits_.begin(), + translationUnits_.end(), + std::back_inserter(translationUnits), + filePathCompare); + + return translationUnits; } std::vector::const_iterator TranslationUnits::findTranslationUnit(const Utf8String &filePath, const Utf8String &projectPartId) const diff --git a/src/tools/clangbackend/ipcsource/translationunits.h b/src/tools/clangbackend/ipcsource/translationunits.h index d30ca982504..e8fea785bd8 100644 --- a/src/tools/clangbackend/ipcsource/translationunits.h +++ b/src/tools/clangbackend/ipcsource/translationunits.h @@ -94,7 +94,7 @@ private: TranslationUnit createTranslationUnit(const FileContainer &fileContainer); void updateTranslationUnit(const FileContainer &fileContainer); std::vector::iterator findTranslationUnit(const FileContainer &fileContainer); - std::vector::iterator findAllTranslationUnitWithFilePath(const Utf8String &filePath); + std::vector findAllTranslationUnitWithFilePath(const Utf8String &filePath); std::vector::const_iterator findTranslationUnit(const Utf8String &filePath, const Utf8String &projectPartId) const; bool hasTranslationUnit(const FileContainer &fileContainer) const; bool hasTranslationUnitWithFilePath(const Utf8String &filePath) const; diff --git a/tests/unit/unittest/translationunitstest.cpp b/tests/unit/unittest/translationunitstest.cpp index 94599b0a222..220cb81a178 100644 --- a/tests/unit/unittest/translationunitstest.cpp +++ b/tests/unit/unittest/translationunitstest.cpp @@ -99,6 +99,7 @@ protected: const Utf8String headerPath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.h"); const Utf8String nonExistingFilePath = Utf8StringLiteral("foo.cpp"); const Utf8String projectPartId = Utf8StringLiteral("projectPartId"); + const Utf8String otherProjectPartId = Utf8StringLiteral("otherProjectPartId"); const Utf8String nonExistingProjectPartId = Utf8StringLiteral("nonExistingProjectPartId"); const ClangBackEnd::FileContainer fileContainer{filePath, projectPartId}; const ClangBackEnd::FileContainer headerContainer{headerPath, projectPartId}; @@ -178,7 +179,7 @@ TEST_F(TranslationUnits, ThrowForUpdatingANonExistingTranslationUnit) ClangBackEnd::TranslationUnitDoesNotExistException); } -TEST_F(TranslationUnits, Update) +TEST_F(TranslationUnits, UpdateSingle) { ClangBackEnd::FileContainer createFileContainer(filePath, projectPartId, Utf8StringVector(), 74u); ClangBackEnd::FileContainer updateFileContainer(filePath, Utf8String(), Utf8StringVector(), 75u); @@ -190,6 +191,21 @@ TEST_F(TranslationUnits, Update) IsTranslationUnit(filePath, projectPartId, 75u)); } +TEST_F(TranslationUnits, UpdateMultiple) +{ + ClangBackEnd::FileContainer fileContainer(filePath, projectPartId, Utf8StringVector(), 74u); + ClangBackEnd::FileContainer fileContainerWithOtherProject(filePath, otherProjectPartId, Utf8StringVector(), 74u); + ClangBackEnd::FileContainer updatedFileContainer(filePath, Utf8String(), Utf8StringVector(), 75u); + translationUnits.create({fileContainer, fileContainerWithOtherProject}); + + translationUnits.update({updatedFileContainer}); + + ASSERT_THAT(translationUnits.translationUnit(filePath, projectPartId), + IsTranslationUnit(filePath, projectPartId, 75u)); + ASSERT_THAT(translationUnits.translationUnit(filePath, otherProjectPartId), + IsTranslationUnit(filePath, otherProjectPartId, 75u)); +} + TEST_F(TranslationUnits, UpdateUnsavedFileAndCheckForReparse) { ClangBackEnd::FileContainer fileContainer(filePath, projectPartId, Utf8StringVector(), 74u); @@ -509,6 +525,7 @@ TEST_F(TranslationUnits, SendDocumentAnnotationsAfterProjectPartChange) void TranslationUnits::SetUp() { projects.createOrUpdate({ProjectPartContainer(projectPartId)}); + projects.createOrUpdate({ProjectPartContainer(otherProjectPartId)}); auto callback = [&] (const DiagnosticsChangedMessage &, const HighlightingChangedMessage &) { mockSendDocumentAnnotationsCallback.sendDocumentAnnotations();