diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp b/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp index 23d32307..11c77221 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp +++ b/src/lib_cxx/data/indexer/IndexerCommandCxx.cpp @@ -10,23 +10,13 @@ #include "OrderedCache.h" #include "ResourcePaths.h" #include "utility.h" +#include "utilitySourceGroupCxx.h" #include "utilityString.h" -std::shared_ptr IndexerCommandCxx::loadCDB(const FilePath& cdbPath) +std::vector IndexerCommandCxx::getSourceFilesFromCDB(const FilePath& cdbPath) { - if (cdbPath.empty() || !cdbPath.exists()) - { - return nullptr; - } - std::string error; - std::shared_ptr cdb = std::shared_ptr( - clang::tooling::JSONCompilationDatabase::loadFromFile( - utility::encodeToUtf8(cdbPath.wstr()), - error, - clang::tooling::JSONCommandLineSyntax::AutoDetect - ) - ); + std::shared_ptr cdb = utility::loadCDB(cdbPath, &error); if (!error.empty()) { @@ -35,12 +25,7 @@ std::shared_ptr IndexerCommandCxx::load MessageStatus(message, true).dispatch(); } - return cdb; -} - -std::vector IndexerCommandCxx::getSourceFilesFromCDB(const FilePath& cdbPath) -{ - return getSourceFilesFromCDB(loadCDB(cdbPath), cdbPath); + return getSourceFilesFromCDB(cdb, cdbPath); } std::vector IndexerCommandCxx::getSourceFilesFromCDB( diff --git a/src/lib_cxx/data/indexer/IndexerCommandCxx.h b/src/lib_cxx/data/indexer/IndexerCommandCxx.h index 67167d9a..5ec28060 100644 --- a/src/lib_cxx/data/indexer/IndexerCommandCxx.h +++ b/src/lib_cxx/data/indexer/IndexerCommandCxx.h @@ -17,7 +17,6 @@ class IndexerCommandCxx : public IndexerCommand { public: - static std::shared_ptr loadCDB(const FilePath& cdbPath); static std::vector getSourceFilesFromCDB(const FilePath& cdbPath); static std::vector getSourceFilesFromCDB( std::shared_ptr cdb, const FilePath& cdbPath); diff --git a/src/lib_cxx/data/parser/cxx/utilityClang.h b/src/lib_cxx/data/parser/cxx/utilityClang.h index 0fad3b06..9f027415 100644 --- a/src/lib_cxx/data/parser/cxx/utilityClang.h +++ b/src/lib_cxx/data/parser/cxx/utilityClang.h @@ -9,6 +9,7 @@ struct ParseLocation; struct ParseLocation; class CanonicalFilePathCache; +class FilePath; namespace clang { diff --git a/src/lib_cxx/project/SourceGroupCxxCdb.cpp b/src/lib_cxx/project/SourceGroupCxxCdb.cpp index e2c93e59..f44b8ad7 100644 --- a/src/lib_cxx/project/SourceGroupCxxCdb.cpp +++ b/src/lib_cxx/project/SourceGroupCxxCdb.cpp @@ -47,7 +47,7 @@ std::set SourceGroupCxxCdb::filterToContainedFilePaths(const std::set< std::set SourceGroupCxxCdb::getAllSourceFilePaths() const { - return getAllSourceFilePaths(IndexerCommandCxx::loadCDB(m_settings->getCompilationDatabasePathExpandedAndAbsolute())); + return getAllSourceFilePaths(utility::loadCDB(m_settings->getCompilationDatabasePathExpandedAndAbsolute())); } std::set SourceGroupCxxCdb::getAllSourceFilePaths(std::shared_ptr cdb) const @@ -85,7 +85,7 @@ std::shared_ptr SourceGroupCxxCdb::getIndexerCommandProv std::shared_ptr provider = std::make_shared(); const FilePath cdbPath = m_settings->getCompilationDatabasePathExpandedAndAbsolute(); - std::shared_ptr cdb = IndexerCommandCxx::loadCDB(cdbPath); + std::shared_ptr cdb = utility::loadCDB(cdbPath); if (!cdb) { return provider; @@ -161,7 +161,7 @@ std::shared_ptr SourceGroupCxxCdb::getPreIndexTask( if (m_settings->getUseCompilerFlags()) { const FilePath cdbPath = m_settings->getCompilationDatabasePathExpandedAndAbsolute(); - std::shared_ptr cdb = IndexerCommandCxx::loadCDB(cdbPath); + std::shared_ptr cdb = utility::loadCDB(cdbPath); if (cdb) { const std::set sourceFilePaths = getAllSourceFilePaths(cdb); diff --git a/src/lib_cxx/project/utilitySourceGroupCxx.cpp b/src/lib_cxx/project/utilitySourceGroupCxx.cpp index a3ffaeee..a59cfa23 100644 --- a/src/lib_cxx/project/utilitySourceGroupCxx.cpp +++ b/src/lib_cxx/project/utilitySourceGroupCxx.cpp @@ -99,6 +99,30 @@ namespace utility ); } + std::shared_ptr loadCDB(const FilePath& cdbPath, std::string* error) + { + if (cdbPath.empty() || !cdbPath.exists()) + { + return std::shared_ptr(); + } + + std::string errorString; + std::shared_ptr cdb = std::shared_ptr( + clang::tooling::JSONCompilationDatabase::loadFromFile( + utility::encodeToUtf8(cdbPath.wstr()), + errorString, + clang::tooling::JSONCommandLineSyntax::AutoDetect + ) + ); + + if (error && !errorString.empty()) + { + *error = errorString; + } + + return cdb; + } + bool containsIncludePchFlags(std::shared_ptr cdb) { for (const clang::tooling::CompileCommand& command : cdb->getAllCompileCommands()) diff --git a/src/lib_cxx/project/utilitySourceGroupCxx.h b/src/lib_cxx/project/utilitySourceGroupCxx.h index 1e4c077c..0f89fb13 100644 --- a/src/lib_cxx/project/utilitySourceGroupCxx.h +++ b/src/lib_cxx/project/utilitySourceGroupCxx.h @@ -12,6 +12,7 @@ namespace clang { } class DialogView; +class FilePath; class SourceGroupSettingsWithCxxPchOptions; class StorageProvider; class Task; @@ -24,6 +25,7 @@ namespace utility std::shared_ptr storageProvider, std::shared_ptr dialogView); + std::shared_ptr loadCDB(const FilePath& cdbPath, std::string* error = nullptr); bool containsIncludePchFlags(std::shared_ptr cdb); bool containsIncludePchFlag(const std::vector& args); std::vector getWithRemoveIncludePchFlag(const std::vector& args); diff --git a/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.cpp b/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.cpp index 8bddf6dd..20112faf 100644 --- a/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.cpp +++ b/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.cpp @@ -4,6 +4,7 @@ #include "SourceGroupCxxCdb.h" #include "SourceGroupSettingsCxxCdb.h" #include "utility.h" +#include "utilitySourceGroupCxx.h" #include "utilityFile.h" QtProjectWizardContentPathCDB::QtProjectWizardContentPathCDB( @@ -37,6 +38,7 @@ void QtProjectWizardContentPathCDB::populate(QGridLayout* layout, int& row) m_picker->setPickDirectory(false); m_picker->setFileFilter("JSON Compilation Database (*.json)"); connect(m_picker, &QtLocationPicker::locationPicked, this, &QtProjectWizardContentPathCDB::pickedPath); + connect(m_picker, &QtLocationPicker::textChanged, this, &QtProjectWizardContentPathCDB::onPickerTextChanged); QLabel* description = new QLabel( "Sourcetrail will use all include paths and compiler flags from the Compilation Database and stay up-to-date " @@ -112,6 +114,21 @@ void QtProjectWizardContentPathCDB::pickedPath() m_window->loadContent(); } +void QtProjectWizardContentPathCDB::onPickerTextChanged(const QString& text) +{ + const FilePath cdbPath = utility::getExpandedAndAbsolutePath(FilePath(text.toStdWString()), m_settings->getProjectDirectoryPath()); + if (!cdbPath.empty() && cdbPath.exists() && + cdbPath != m_settings->getCompilationDatabasePathExpandedAndAbsolute()) + { + std::string error; + std::shared_ptr cdb = utility::loadCDB(cdbPath, &error); + if (cdb && error.empty()) + { + pickedPath(); + } + } +} + std::shared_ptr QtProjectWizardContentPathCDB::getSourceGroupSettings() { return m_settings; diff --git a/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.h b/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.h index fddd4ac7..f6bf8e4c 100644 --- a/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.h +++ b/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCDB.h @@ -27,6 +27,7 @@ public: private slots: void pickedPath(); + void onPickerTextChanged(const QString& text); private: std::shared_ptr getSourceGroupSettings() override; diff --git a/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCxxPch.cpp b/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCxxPch.cpp index 5743b17a..aafa1dfe 100644 --- a/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCxxPch.cpp +++ b/src/lib_gui/qt/project_wizard/content/path/QtProjectWizardContentPathCxxPch.cpp @@ -53,7 +53,7 @@ bool QtProjectWizardContentPathCxxPch::check() if (std::shared_ptr cdbSettings = std::dynamic_pointer_cast(m_settings)) { const FilePath cdbPath = cdbSettings->getCompilationDatabasePathExpandedAndAbsolute(); - std::shared_ptr cdb = IndexerCommandCxx::loadCDB(cdbPath); + std::shared_ptr cdb = utility::loadCDB(cdbPath); if (!cdb) { QMessageBox msgBox;