C++: move post-sourceprocessing action into callback.

Change-Id: Iac6c9fe1ada27ac0d96417e490cc5723e6969541
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
Erik Verbruggen
2014-06-06 14:41:19 +02:00
parent 528fa525bc
commit 13d2d499ab
8 changed files with 58 additions and 56 deletions

View File

@@ -84,7 +84,7 @@ static void parse(QFutureInterface<void> &future,
} }
future.setProgressValue(files.size()); future.setProgressValue(files.size());
sourceProcessor->modelManager()->finishedRefreshingSourceFiles(files); cmm->finishedRefreshingSourceFiles(files);
delete sourceProcessor; delete sourceProcessor;
} }
@@ -184,7 +184,8 @@ QFuture<void> BuiltinIndexingSupport::refreshSourceFiles(const QStringList &sour
CppModelManager *mgr = CppModelManager::instance(); CppModelManager *mgr = CppModelManager::instance();
const WorkingCopy workingCopy = mgr->workingCopy(); const WorkingCopy workingCopy = mgr->workingCopy();
CppSourceProcessor *preproc = new CppSourceProcessor(mgr, m_dumpFileNameWhileParsing); CppSourceProcessor *preproc = CppModelManager::createSourceProcessor();
preproc->setDumpFileNameWhileParsing(m_dumpFileNameWhileParsing);
preproc->setRevision(++m_revision); preproc->setRevision(++m_revision);
preproc->setIncludePaths(mgr->includePaths()); preproc->setIncludePaths(mgr->includePaths());
preproc->setFrameworkPaths(mgr->frameworkPaths()); preproc->setFrameworkPaths(mgr->frameworkPaths());

View File

@@ -160,6 +160,24 @@ QStringList CppModelManager::timeStampModifiedFiles(const QList<Document::Ptr> &
return sourceFiles; return sourceFiles;
} }
/*!
* \brief createSourceProcessor Create a new source processor, which will signal the
* model manager when a document has been processed.
*
* Indexed file is truncated version of fully parsed document: copy of source
* code and full AST will be dropped when indexing is done.
*
* \return a new source processor object, which the caller needs to delete when finished.
*/
CppSourceProcessor *CppModelManager::createSourceProcessor()
{
CppModelManager *that = instance();
return new CppSourceProcessor(that->snapshot(), [=](const Document::Ptr &doc) {
that->emitDocumentUpdated(doc);
doc->releaseSourceAndAST();
});
}
void CppModelManager::updateModifiedSourceFiles() void CppModelManager::updateModifiedSourceFiles()
{ {
const Snapshot snapshot = this->snapshot(); const Snapshot snapshot = this->snapshot();

View File

@@ -49,6 +49,7 @@ class CppEditorSupport;
namespace Internal { namespace Internal {
class CppFindReferences; class CppFindReferences;
class CppSourceProcessor;
class CppModelManager : public CppTools::CppModelManagerInterface class CppModelManager : public CppTools::CppModelManagerInterface
{ {
@@ -155,6 +156,8 @@ public:
static QStringList timeStampModifiedFiles(const QList<Document::Ptr> &documentsToCheck); static QStringList timeStampModifiedFiles(const QList<Document::Ptr> &documentsToCheck);
static CppSourceProcessor *createSourceProcessor();
signals: signals:
void gcFinished(); // Needed for tests. void gcFinished(); // Needed for tests.

View File

@@ -159,7 +159,17 @@ void SnapshotUpdater::update(CppModelManager::WorkingCopy workingCopy)
workingCopy.insert(editorDefinesFileName, m_editorDefines); workingCopy.insert(editorDefinesFileName, m_editorDefines);
} }
CppSourceProcessor sourceProcessor(modelManager, m_snapshot); CppSourceProcessor sourceProcessor(m_snapshot, [&](const Document::Ptr &doc) {
const QString fileName = doc->fileName();
const bool isInEditor = fileName == fileInEditor();
Document::Ptr otherDoc = modelManager->document(fileName);
unsigned newRev = otherDoc.isNull() ? 1U : otherDoc->revision() + 1;
if (isInEditor)
newRev = qMax(rev + 1, newRev);
doc->setRevision(newRev);
modelManager->emitDocumentUpdated(doc);
doc->releaseSourceAndAST();
});
Snapshot globalSnapshot = modelManager->snapshot(); Snapshot globalSnapshot = modelManager->snapshot();
globalSnapshot.remove(fileInEditor()); globalSnapshot.remove(fileInEditor());
sourceProcessor.setGlobalSnapshot(globalSnapshot); sourceProcessor.setGlobalSnapshot(globalSnapshot);
@@ -185,20 +195,6 @@ void SnapshotUpdater::update(CppModelManager::WorkingCopy workingCopy)
} }
m_snapshot = newSnapshot; m_snapshot = newSnapshot;
m_deps.build(m_snapshot); m_deps.build(m_snapshot);
foreach (Document::Ptr doc, m_snapshot) {
QString fileName = doc->fileName();
if (doc->revision() == 0) {
Document::Ptr otherDoc = globalSnapshot.document(fileName);
doc->setRevision(otherDoc.isNull() ? 0 : otherDoc->revision());
}
if (fileName != fileInEditor())
doc->releaseSourceAndAST();
}
QTC_CHECK(document());
if (Document::Ptr doc = document())
doc->setRevision(rev + 1);
} }
} }

View File

@@ -17,9 +17,8 @@
* \class CppTools::Internal::CppSourceProcessor * \class CppTools::Internal::CppSourceProcessor
* \brief The CppSourceProcessor class updates set of indexed C++ files. * \brief The CppSourceProcessor class updates set of indexed C++ files.
* *
* Indexed file is truncated version of fully parsed document: copy of source * Working copy ensures that documents with most recent copy placed in memory will be parsed
* code and full AST will be dropped when indexing is done. Working copy ensures * correctly.
* that documents with most recent copy placed in memory will be parsed correctly.
* *
* \sa CPlusPlus::Document * \sa CPlusPlus::Document
* \sa CppTools::CppModelManagerInterface::WorkingCopy * \sa CppTools::CppModelManagerInterface::WorkingCopy
@@ -79,24 +78,10 @@ inline const Macro revision(const CppModelManagerInterface::WorkingCopy &working
} // anonymous namespace } // anonymous namespace
CppSourceProcessor::CppSourceProcessor(QPointer<CppModelManager> modelManager, CppSourceProcessor::CppSourceProcessor(const Snapshot &snapshot, DocumentCallback documentFinished)
bool dumpFileNameWhileParsing)
: m_snapshot(modelManager->snapshot()),
m_modelManager(modelManager),
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing),
m_preprocess(this, &m_env),
m_revision(0),
m_defaultCodec(Core::EditorManager::defaultTextCodec())
{
m_preprocess.setKeepComments(true);
}
CppSourceProcessor::CppSourceProcessor(QPointer<CppModelManager> modelManager,
const Snapshot &snapshot,
bool dumpFileNameWhileParsing)
: m_snapshot(snapshot), : m_snapshot(snapshot),
m_modelManager(modelManager), m_documentFinished(documentFinished),
m_dumpFileNameWhileParsing(dumpFileNameWhileParsing), m_dumpFileNameWhileParsing(false),
m_preprocess(this, &m_env), m_preprocess(this, &m_env),
m_revision(0), m_revision(0),
m_defaultCodec(Core::EditorManager::defaultTextCodec()) m_defaultCodec(Core::EditorManager::defaultTextCodec())
@@ -417,7 +402,7 @@ void CppSourceProcessor::sourceNeeded(unsigned line, const QString &fileName, In
} }
if (m_included.contains(absoluteFileName)) if (m_included.contains(absoluteFileName))
return; // We've already seen this file. return; // We've already seen this file.
if (absoluteFileName != modelManager()->configurationFileName()) if (!isInjectedFile(absoluteFileName))
m_included.insert(absoluteFileName); m_included.insert(absoluteFileName);
// Already in snapshot? Use it! // Already in snapshot? Use it!
@@ -470,10 +455,7 @@ void CppSourceProcessor::sourceNeeded(unsigned line, const QString &fileName, In
document->check(m_workingCopy.contains(document->fileName()) ? Document::FullCheck document->check(m_workingCopy.contains(document->fileName()) ? Document::FullCheck
: Document::FastCheck); : Document::FastCheck);
if (m_modelManager) { m_documentFinished(document);
m_modelManager->emitDocumentUpdated(document);
document->releaseSourceAndAST();
}
m_snapshot.insert(document); m_snapshot.insert(document);
m_todo.remove(absoluteFileName); m_todo.remove(absoluteFileName);

View File

@@ -5,6 +5,7 @@
#include <cplusplus/PreprocessorEnvironment.h> #include <cplusplus/PreprocessorEnvironment.h>
#include <cplusplus/pp-engine.h> #include <cplusplus/pp-engine.h>
#include <utils/function.h>
#include <utils/qtcoverride.h> #include <utils/qtcoverride.h>
#include <QHash> #include <QHash>
@@ -19,21 +20,23 @@ QT_END_NAMESPACE
namespace CppTools { namespace CppTools {
namespace Internal { namespace Internal {
class CppModelManager;
// Documentation inside. // Documentation inside.
class CppSourceProcessor: public CPlusPlus::Client class CppSourceProcessor: public CPlusPlus::Client
{ {
Q_DISABLE_COPY(CppSourceProcessor) Q_DISABLE_COPY(CppSourceProcessor)
public:
typedef std::function<void (const CPlusPlus::Document::Ptr &)> DocumentCallback;
public: public:
static QString cleanPath(const QString &path); static QString cleanPath(const QString &path);
CppSourceProcessor(QPointer<CppModelManager> modelManager, bool dumpFileNameWhileParsing = false); CppSourceProcessor(const CPlusPlus::Snapshot &snapshot, DocumentCallback documentFinished);
CppSourceProcessor(QPointer<CppModelManager> modelManager, const CPlusPlus::Snapshot &snapshot,
bool dumpFileNameWhileParsing = false);
~CppSourceProcessor(); ~CppSourceProcessor();
void setDumpFileNameWhileParsing(bool onoff)
{ m_dumpFileNameWhileParsing = onoff; }
void setRevision(unsigned revision); void setRevision(unsigned revision);
void setWorkingCopy(const CppTools::CppModelManagerInterface::WorkingCopy &workingCopy); void setWorkingCopy(const CppTools::CppModelManagerInterface::WorkingCopy &workingCopy);
void setIncludePaths(const QStringList &includePaths); void setIncludePaths(const QStringList &includePaths);
@@ -46,12 +49,10 @@ public:
CPlusPlus::Snapshot snapshot() const { return m_snapshot; } CPlusPlus::Snapshot snapshot() const { return m_snapshot; }
const QSet<QString> &todo() const { return m_todo; } const QSet<QString> &todo() const { return m_todo; }
CppModelManager *modelManager() const { return m_modelManager.data(); }
void setGlobalSnapshot(const CPlusPlus::Snapshot &snapshot) { m_globalSnapshot = snapshot; } void setGlobalSnapshot(const CPlusPlus::Snapshot &snapshot) { m_globalSnapshot = snapshot; }
private: private:
CppSourceProcessor();
void addFrameworkPath(const QString &frameworkPath); void addFrameworkPath(const QString &frameworkPath);
CPlusPlus::Document::Ptr switchCurrentDocument(CPlusPlus::Document::Ptr doc); CPlusPlus::Document::Ptr switchCurrentDocument(CPlusPlus::Document::Ptr doc);
@@ -84,7 +85,7 @@ private:
private: private:
CPlusPlus::Snapshot m_snapshot; CPlusPlus::Snapshot m_snapshot;
CPlusPlus::Snapshot m_globalSnapshot; CPlusPlus::Snapshot m_globalSnapshot;
QPointer<CppModelManager> m_modelManager; DocumentCallback m_documentFinished;
bool m_dumpFileNameWhileParsing; bool m_dumpFileNameWhileParsing;
CPlusPlus::Environment m_env; CPlusPlus::Environment m_env;
CPlusPlus::Preprocessor m_preprocess; CPlusPlus::Preprocessor m_preprocess;

View File

@@ -69,9 +69,10 @@ public:
if (!scopedFile.writtenSuccessfully()) if (!scopedFile.writtenSuccessfully())
return Document::Ptr(); return Document::Ptr();
CppSourceProcessor sourceProcessor((QPointer<CppModelManager>(m_cmm))); QScopedPointer<CppSourceProcessor> sourceProcessor(
sourceProcessor.setIncludePaths(QStringList(TestIncludePaths::directoryOfTestFile())); CppModelManager::createSourceProcessor());
sourceProcessor.run(fileName); sourceProcessor->setIncludePaths(QStringList(TestIncludePaths::directoryOfTestFile()));
sourceProcessor->run(fileName);
Document::Ptr document = m_cmm->document(fileName); Document::Ptr document = m_cmm->document(fileName);
return document; return document;

View File

@@ -517,9 +517,9 @@ static QList<Include> includesForSource(const QByteArray &source)
using namespace CppTools::Internal; using namespace CppTools::Internal;
CppModelManager *cmm = CppModelManager::instance(); CppModelManager *cmm = CppModelManager::instance();
cmm->GC(); cmm->GC();
CppSourceProcessor sourceProcessor((QPointer<CppModelManager>(cmm))); QScopedPointer<CppSourceProcessor> sourceProcessor(CppModelManager::createSourceProcessor());
sourceProcessor.setIncludePaths(QStringList(TestIncludePaths::globalIncludePath())); sourceProcessor->setIncludePaths(QStringList(TestIncludePaths::globalIncludePath()));
sourceProcessor.run(fileName); sourceProcessor->run(fileName);
Document::Ptr document = cmm->document(fileName); Document::Ptr document = cmm->document(fileName);
return document->resolvedIncludes(); return document->resolvedIncludes();