diff options
| author | Erik Verbruggen <[email protected]> | 2012-10-16 16:02:40 +0200 |
|---|---|---|
| committer | Erik Verbruggen <[email protected]> | 2012-10-23 09:26:19 +0200 |
| commit | 9246e78d239dbe23572f2a29961c60ea1f27fe6c (patch) | |
| tree | b0c6a3ad0913b5e8369e68374f3c694e06da0a23 /src/plugins/cpptools/cppmodelmanager.cpp | |
| parent | 80b737252b8b01525805f48bf95678ea4835b294 (diff) | |
C++: started to make indexing plug-able.
Change-Id: Iff35840d73882a212dd77b54e4223c0b4111ac1c
Reviewed-by: Erik Verbruggen <[email protected]>
Diffstat (limited to 'src/plugins/cpptools/cppmodelmanager.cpp')
| -rw-r--r-- | src/plugins/cpptools/cppmodelmanager.cpp | 274 |
1 files changed, 156 insertions, 118 deletions
diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp index 16f1f95d4d2..4f2902bcddc 100644 --- a/src/plugins/cpptools/cppmodelmanager.cpp +++ b/src/plugins/cpptools/cppmodelmanager.cpp @@ -34,6 +34,7 @@ #include "cppcompletionassist.h" #include "cpphighlightingsupport.h" #include "cpphighlightingsupportinternal.h" +#include "cppindexingsupport.h" #include "abstracteditorsupport.h" #include "cpptoolsconstants.h" #include "cpptoolseditorsupport.h" @@ -661,6 +662,144 @@ void CppModelManager::updateModifiedSourceFiles() updateSourceFiles(sourceFiles); } +namespace { + +class IndexingSupport: public CppIndexingSupport { +public: + typedef CppModelManagerInterface::WorkingCopy WorkingCopy; + +public: + IndexingSupport() + : m_revision(0) + { + m_synchronizer.setCancelOnWait(true); + m_dumpFileNameWhileParsing = !qgetenv("QTCREATOR_DUMP_FILENAME_WHILE_PARSING").isNull(); + } + + ~IndexingSupport() + {} + + QFuture<void> refreshSourceFiles(const QStringList &sourceFiles) + { + CppModelManager *mgr = CppModelManager::instance(); + const WorkingCopy workingCopy = mgr->workingCopy(); + + CppPreprocessor *preproc = new CppPreprocessor(mgr, m_dumpFileNameWhileParsing); + preproc->setRevision(++m_revision); + preproc->setProjectFiles(mgr->projectFiles()); + preproc->setIncludePaths(mgr->includePaths()); + preproc->setFrameworkPaths(mgr->frameworkPaths()); + preproc->setWorkingCopy(workingCopy); + + QFuture<void> result = QtConcurrent::run(&parse, preproc, sourceFiles); + + if (m_synchronizer.futures().size() > 10) { + QList<QFuture<void> > futures = m_synchronizer.futures(); + + m_synchronizer.clearFutures(); + + foreach (const QFuture<void> &future, futures) { + if (! (future.isFinished() || future.isCanceled())) + m_synchronizer.addFuture(future); + } + } + + m_synchronizer.addFuture(result); + + if (sourceFiles.count() > 1) { + Core::ICore::progressManager()->addTask(result, + QCoreApplication::translate("IndexingSupport", "Parsing"), + CppTools::Constants::TASK_INDEX); + } + + return result; + } + +private: + static void parse(QFutureInterface<void> &future, + CppPreprocessor *preproc, + QStringList files) + { + if (files.isEmpty()) + return; + + const Core::MimeDatabase *mimeDb = Core::ICore::mimeDatabase(); + Core::MimeType cSourceTy = mimeDb->findByType(QLatin1String("text/x-csrc")); + Core::MimeType cppSourceTy = mimeDb->findByType(QLatin1String("text/x-c++src")); + Core::MimeType mSourceTy = mimeDb->findByType(QLatin1String("text/x-objcsrc")); + + QStringList sources; + QStringList headers; + + QStringList suffixes = cSourceTy.suffixes(); + suffixes += cppSourceTy.suffixes(); + suffixes += mSourceTy.suffixes(); + + foreach (const QString &file, files) { + QFileInfo info(file); + + preproc->snapshot.remove(file); + + if (suffixes.contains(info.suffix())) + sources.append(file); + else + headers.append(file); + } + + const int sourceCount = sources.size(); + files = sources; + files += headers; + + preproc->setTodo(files); + + future.setProgressRange(0, files.size()); + + QString conf = QLatin1String(pp_configuration_file); + + bool processingHeaders = false; + + for (int i = 0; i < files.size(); ++i) { + if (future.isPaused()) + future.waitForResume(); + + if (future.isCanceled()) + break; + + const QString fileName = files.at(i); + + const bool isSourceFile = i < sourceCount; + if (isSourceFile) + (void) preproc->run(conf); + + else if (! processingHeaders) { + (void) preproc->run(conf); + + processingHeaders = true; + } + + preproc->run(fileName); + + future.setProgressValue(files.size() - preproc->todo().size()); + + if (isSourceFile) + preproc->resetEnvironment(); + } + + future.setProgressValue(files.size()); + preproc->modelManager()->finishedRefreshingSourceFiles(files); + + delete preproc; + } + +private: + QFutureSynchronizer<void> m_synchronizer; + unsigned m_revision; + bool m_dumpFileNameWhileParsing; +}; + + +} // anonymous namespace + /*! \class CppTools::CppModelManager \brief The CppModelManager keeps track of one CppCodeModel instance @@ -689,10 +828,6 @@ CppModelManager::CppModelManager(QObject *parent) { m_findReferences = new CppFindReferences(this); m_indexerEnabled = qgetenv("QTCREATOR_NO_CODE_INDEXER").isNull(); - m_dumpFileNameWhileParsing = !qgetenv("QTCREATOR_DUMP_FILENAME_WHILE_PARSING").isNull(); - - m_revision = 0; - m_synchronizer.setCancelOnWait(true); m_dirty = true; @@ -737,6 +872,7 @@ CppModelManager::CppModelManager(QObject *parent) ExtensionSystem::PluginManager::addObject(m_completionAssistProvider); m_highlightingFallback = new CppHighlightingSupportInternalFactory; m_highlightingFactory = m_highlightingFallback; + m_internalIndexingSupport = new IndexingSupport; } CppModelManager::~CppModelManager() @@ -744,6 +880,7 @@ CppModelManager::~CppModelManager() ExtensionSystem::PluginManager::removeObject(m_completionAssistProvider); delete m_completionFallback; delete m_highlightingFallback; + delete m_internalIndexingSupport; } Snapshot CppModelManager::snapshot() const @@ -899,7 +1036,15 @@ CppModelManager::WorkingCopy CppModelManager::workingCopy() const } QFuture<void> CppModelManager::updateSourceFiles(const QStringList &sourceFiles) -{ return refreshSourceFiles(sourceFiles); } +{ + if (sourceFiles.isEmpty() || !m_indexerEnabled) + return QFuture<void>(); + + foreach (CppIndexingSupport *indexer, m_indexingSupporters) + indexer->refreshSourceFiles(sourceFiles); + + return m_internalIndexingSupport->refreshSourceFiles(sourceFiles); +} QList<CppModelManager::ProjectInfo> CppModelManager::projectInfos() const { @@ -979,44 +1124,6 @@ QList<CppModelManager::ProjectPart::Ptr> CppModelManager::projectPart(const QStr return parts; } -QFuture<void> CppModelManager::refreshSourceFiles(const QStringList &sourceFiles) -{ - if (! sourceFiles.isEmpty() && m_indexerEnabled) { - const WorkingCopy workingCopy = buildWorkingCopyList(); - - CppPreprocessor *preproc = new CppPreprocessor(this, m_dumpFileNameWhileParsing); - preproc->setRevision(++m_revision); - preproc->setProjectFiles(projectFiles()); - preproc->setIncludePaths(includePaths()); - preproc->setFrameworkPaths(frameworkPaths()); - preproc->setWorkingCopy(workingCopy); - - QFuture<void> result = QtConcurrent::run(&CppModelManager::parse, - preproc, sourceFiles); - - if (m_synchronizer.futures().size() > 10) { - QList<QFuture<void> > futures = m_synchronizer.futures(); - - m_synchronizer.clearFutures(); - - foreach (const QFuture<void> &future, futures) { - if (! (future.isFinished() || future.isCanceled())) - m_synchronizer.addFuture(future); - } - } - - m_synchronizer.addFuture(result); - - if (sourceFiles.count() > 1) { - Core::ICore::progressManager()->addTask(result, tr("Parsing"), - CppTools::Constants::TASK_INDEX); - } - - return result; - } - return QFuture<void>(); -} - /*! \fn void CppModelManager::editorOpened(Core::IEditor *editor) \brief If a C++ editor is opened, the model manager listens to content changes @@ -1243,81 +1350,6 @@ void CppModelManager::onAboutToUnloadSession() GC(); } -void CppModelManager::parse(QFutureInterface<void> &future, - CppPreprocessor *preproc, - QStringList files) -{ - if (files.isEmpty()) - return; - - const Core::MimeDatabase *mimeDb = Core::ICore::mimeDatabase(); - Core::MimeType cSourceTy = mimeDb->findByType(QLatin1String("text/x-csrc")); - Core::MimeType cppSourceTy = mimeDb->findByType(QLatin1String("text/x-c++src")); - Core::MimeType mSourceTy = mimeDb->findByType(QLatin1String("text/x-objcsrc")); - - QStringList sources; - QStringList headers; - - QStringList suffixes = cSourceTy.suffixes(); - suffixes += cppSourceTy.suffixes(); - suffixes += mSourceTy.suffixes(); - - foreach (const QString &file, files) { - QFileInfo info(file); - - preproc->snapshot.remove(file); - - if (suffixes.contains(info.suffix())) - sources.append(file); - else - headers.append(file); - } - - const int sourceCount = sources.size(); - files = sources; - files += headers; - - preproc->setTodo(files); - - future.setProgressRange(0, files.size()); - - QString conf = QLatin1String(pp_configuration_file); - - bool processingHeaders = false; - - for (int i = 0; i < files.size(); ++i) { - if (future.isPaused()) - future.waitForResume(); - - if (future.isCanceled()) - break; - - const QString fileName = files.at(i); - - const bool isSourceFile = i < sourceCount; - if (isSourceFile) - (void) preproc->run(conf); - - else if (! processingHeaders) { - (void) preproc->run(conf); - - processingHeaders = true; - } - - preproc->run(fileName); - - future.setProgressValue(files.size() - preproc->todo().size()); - - if (isSourceFile) - preproc->resetEnvironment(); - } - - future.setProgressValue(files.size()); - preproc->modelManager()->finishedRefreshingSourceFiles(files); - - delete preproc; -} - void CppModelManager::GC() { protectSnapshot.lock(); @@ -1399,6 +1431,12 @@ void CppModelManager::setHighlightingSupportFactory(CppHighlightingSupportFactor m_highlightingFactory = m_highlightingFallback; } +void CppModelManager::addIndexingSupport(CppIndexingSupport *indexingSupport) +{ + if (indexingSupport) + m_indexingSupporters.append(indexingSupport); +} + void CppModelManager::setExtraDiagnostics(const QString &fileName, int kind, const QList<Document::DiagnosticMessage> &diagnostics) { |
