CppTools: Fix snapshot in case another project is opened

When a second project was opened the snapshot was reset. That resulted
in all kinds of problems since from then on the code model effectively
was not aware of the files of the first project.

The regression was introduced by commit a0d6df7b.

Change-Id: I1ccc9de68177205b49a4ba8ead2bc8abe4592b32
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@digia.com>
This commit is contained in:
Nikolai Kosjar
2013-06-28 11:26:02 +02:00
parent 377078ea27
commit 1fad00f999
9 changed files with 157 additions and 10 deletions

View File

@@ -556,23 +556,31 @@ void CppModelManager::updateProjectInfo(const ProjectInfo &pinfo)
return;
ProjectExplorer::Project *project = pinfo.project().data();
m_projects.insert(project, pinfo);
m_dirty = true;
m_srcToProjectPart.clear();
foreach (const ProjectInfo &projectInfo, m_projects) {
foreach (const ProjectPart::Ptr &projectPart, projectInfo.projectParts()) {
ProjectInfo oldProjectInfo = m_projects.value(project);
if (oldProjectInfo.isValid()) {
foreach (const ProjectPart::Ptr &projectPart, oldProjectInfo.projectParts()) {
foreach (const ProjectFile &cxxFile, projectPart->files) {
m_srcToProjectPart[cxxFile.path].append(projectPart);
foreach (const QString &fileName, m_snapshot.allIncludesForDocument(cxxFile.path))
foreach (const QString &fileName,
m_snapshot.allIncludesForDocument(cxxFile.path)) {
m_snapshot.remove(fileName);
}
m_snapshot.remove(cxxFile.path);
}
}
}
m_snapshot.remove(configurationFileName());
m_projects.insert(project, pinfo);
m_dirty = true;
m_srcToProjectPart.clear();
foreach (const ProjectInfo &projectInfo, m_projects) {
foreach (const ProjectPart::Ptr &projectPart, projectInfo.projectParts()) {
foreach (const ProjectFile &cxxFile, projectPart->files) {
m_srcToProjectPart[cxxFile.path].append(projectPart);
}
}
}
}
if (!qgetenv("QTCREATOR_DUMP_PROJECT_INFO").isEmpty())

View File

@@ -96,6 +96,42 @@ private:
const QString m_testDataDirectory;
};
// TODO: When possible, use this helper class in all tests
class ProjectCreator
{
public:
ProjectCreator(ModelManagerTestHelper *modelManagerTestHelper)
: modelManagerTestHelper(modelManagerTestHelper)
{}
/// 'files' is expected to be a list of file names that reside in 'dir'.
void create(const QString &name, const QString &dir, const QStringList files)
{
const TestDataDirectory projectDir(dir);
foreach (const QString &file, files)
projectFiles << projectDir.file(file);
Project *project = modelManagerTestHelper->createProject(name);
projectInfo = CppModelManager::instance()->projectInfo(project);
QCOMPARE(projectInfo.project().data(), project);
ProjectPart::Ptr part(new ProjectPart);
projectInfo.appendProjectPart(part);
part->cxxVersion = ProjectPart::CXX98;
part->qtVersion = ProjectPart::Qt5;
foreach (const QString &file, projectFiles) {
ProjectFile projectFile(file, ProjectFile::classify(file));
part->files.append(projectFile);
}
}
ModelManagerTestHelper *modelManagerTestHelper;
ProjectInfo projectInfo;
QStringList projectFiles;
};
} // anonymous namespace
void CppToolsPlugin::test_modelmanager_paths()
@@ -278,3 +314,49 @@ void CppToolsPlugin::test_modelmanager_refresh_2()
QVERIFY(document->diagnosticMessages().isEmpty());
}
}
void CppToolsPlugin::test_modelmanager_snapshot_after_two_projects()
{
QStringList refreshedFiles;
ModelManagerTestHelper helper;
ProjectCreator project1(&helper);
ProjectCreator project2(&helper);
CppModelManager *mm = CppModelManager::instance();
// Project 1
project1.create(QLatin1String("snapshot_after_two_projects.1"),
QLatin1String("testdata_project1"),
QStringList() << QLatin1String("foo.h")
<< QLatin1String("foo.cpp")
<< QLatin1String("main.cpp"));
mm->updateProjectInfo(project1.projectInfo);
mm->updateSourceFiles(project1.projectFiles);
refreshedFiles = helper.waitForRefreshedSourceFiles();
QCOMPARE(refreshedFiles.toSet(), project1.projectFiles.toSet());
const int snapshotSizeAfterProject1 = mm->snapshot().size();
foreach (const QString &file, project1.projectFiles)
QVERIFY(mm->snapshot().contains(file));
// Project 2
project2.create(QLatin1String("snapshot_after_two_projects.2"),
QLatin1String("testdata_project2"),
QStringList() << QLatin1String("bar.h")
<< QLatin1String("bar.cpp")
<< QLatin1String("main.cpp"));
mm->updateProjectInfo(project2.projectInfo);
mm->updateSourceFiles(project2.projectFiles);
refreshedFiles = helper.waitForRefreshedSourceFiles();
QCOMPARE(refreshedFiles.toSet(), project2.projectFiles.toSet());
const int snapshotSizeAfterProject2 = mm->snapshot().size();
QVERIFY(snapshotSizeAfterProject2 > snapshotSizeAfterProject1);
QVERIFY(snapshotSizeAfterProject2 >= snapshotSizeAfterProject1 + project2.projectFiles.size());
foreach (const QString &file, project1.projectFiles)
QVERIFY(mm->snapshot().contains(file));
foreach (const QString &file, project2.projectFiles)
QVERIFY(mm->snapshot().contains(file));
}

View File

@@ -163,6 +163,7 @@ private slots:
void test_modelmanager_framework_headers();
void test_modelmanager_refresh_1();
void test_modelmanager_refresh_2();
void test_modelmanager_snapshot_after_two_projects();
private:
void test_completion();

View File

@@ -0,0 +1,7 @@
// Copyright header
#include "foo.h"
Foo::Foo()
{
}

View File

@@ -0,0 +1,12 @@
// Copyright header
#ifndef FOO_H
#define FOO_H
class Foo
{
public:
Foo();
};
#endif // FOO_H

View File

@@ -0,0 +1,9 @@
// Copyright header
#include "foo.h"
int main()
{
Foo foo;
return 1;
}

View File

@@ -0,0 +1,7 @@
// Copyright header
#include "bar.h"
Bar::Bar()
{
}

View File

@@ -0,0 +1,12 @@
// Copyright header
#ifndef BAR_H
#define BAR_H
class Bar
{
public:
Bar();
};
#endif // BAR_H

View File

@@ -0,0 +1,9 @@
// Copyright header
#include "bar.h"
int main()
{
Bar bar;
return 1;
}