diff options
author | Ivan Komissarov <abbapoh@gmail.com> | 2020-02-24 22:03:59 +0100 |
---|---|---|
committer | Ivan Komissarov <ABBAPOH@gmail.com> | 2020-02-27 15:18:55 +0000 |
commit | 2ea31e85a8a0edf3442aab2510ad10a5ea6c5685 (patch) | |
tree | 82a038c436fbc873e02105a4f7e30f029f14de92 /src | |
parent | 75e7f721c9661db4e253298533bf399e8320ed3c (diff) |
Simplify ModuleLoader::searchAndLoadModuleFile
The algorithm is more natural now:
- first, we try to find module files in the current searchPaths
- second, if nothing was found, we try to extend the search paths by
using Providers
- third, we try to load module Items from the found files
- finally, choose a candidate or give up if nothing was loaded
This allows to get rid of the recursive call which was needed to execute
the logic from step 3 with newly added search paths as step 3 was done
in the same loop as step 1
Change-Id: Ie6c244c5f649bcb1b96eb2c5edca503439f3f7a7
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/corelib/language/moduleloader.cpp | 70 |
1 files changed, 36 insertions, 34 deletions
diff --git a/src/lib/corelib/language/moduleloader.cpp b/src/lib/corelib/language/moduleloader.cpp index 55e172117..395dfc23b 100644 --- a/src/lib/corelib/language/moduleloader.cpp +++ b/src/lib/corelib/language/moduleloader.cpp @@ -3106,11 +3106,42 @@ Item *ModuleLoader::searchAndLoadModuleFile(ProductContext *productContext, const CodeLocation &dependsItemLocation, const QualifiedId &moduleName, FallbackMode fallbackMode, bool isRequired, Item *moduleInstance) { - bool triedToLoadModule = false; + auto existingPaths = findExistingModulePaths(m_reader->allSearchPaths(), moduleName); + + if (existingPaths.isEmpty()) { // no suitable names found, try to use providers + bool moduleAlreadyKnown = false; + ModuleProviderResult result; + for (QualifiedId providerName = moduleName; !providerName.empty(); + providerName.pop_back()) { + if (!productContext->knownModuleProviders.insert(providerName).second) { + moduleAlreadyKnown = true; + break; + } + qCDebug(lcModuleLoader) << "Module" << moduleName.toString() + << "not found, checking for module providers"; + result = findModuleProvider(providerName, *productContext, + ModuleProviderLookup::Regular, dependsItemLocation); + if (result.providerFound) + break; + } + if (fallbackMode == FallbackMode::Enabled && !result.providerFound + && !moduleAlreadyKnown) { + qCDebug(lcModuleLoader) << "Specific module provider not found for" + << moduleName.toString() << ", setting up fallback."; + result = findModuleProvider(moduleName, *productContext, + ModuleProviderLookup::Fallback, dependsItemLocation); + } + if (result.providerAddedSearchPaths) { + qCDebug(lcModuleLoader) << "Re-checking for module" << moduleName.toString() + << "with newly added search paths from module provider"; + existingPaths = findExistingModulePaths(m_reader->allSearchPaths(), moduleName); + } + } + const QString fullName = moduleName.toString(); + bool triedToLoadModule = false; std::vector<PrioritizedItem> candidates; - const QStringList &searchPaths = m_reader->allSearchPaths(); - const auto existingPaths = findExistingModulePaths(searchPaths, moduleName); + candidates.reserve(size_t(existingPaths.size())); for (int i = 0; i < existingPaths.size(); ++i) { const QString &dirPath = existingPaths.at(i); QStringList &moduleFileNames = getModuleFileNames(dirPath); @@ -3130,41 +3161,12 @@ Item *ModuleLoader::searchAndLoadModuleFile(ProductContext *productContext, } if (candidates.empty()) { - if (existingPaths.isEmpty()) { // no suitable names found, try to use providers - bool moduleAlreadyKnown = false; - ModuleProviderResult result; - for (QualifiedId providerName = moduleName; !providerName.empty(); - providerName.pop_back()) { - if (!productContext->knownModuleProviders.insert(providerName).second) { - moduleAlreadyKnown = true; - break; - } - qCDebug(lcModuleLoader) << "Module" << moduleName.toString() - << "not found, checking for module providers"; - result = findModuleProvider(providerName, *productContext, - ModuleProviderLookup::Regular, dependsItemLocation); - if (result.providerFound) - break; - } - if (fallbackMode == FallbackMode::Enabled && !result.providerFound - && !moduleAlreadyKnown) { - qCDebug(lcModuleLoader) << "Specific module provider not found for" - << moduleName.toString() << ", setting up fallback."; - result = findModuleProvider(moduleName, *productContext, - ModuleProviderLookup::Fallback, dependsItemLocation); - } - if (result.providerAddedSearchPaths) { - qCDebug(lcModuleLoader) << "Re-checking for module" << moduleName.toString() - << "with newly added search paths from module provider"; - return searchAndLoadModuleFile(productContext, dependsItemLocation, moduleName, - fallbackMode, isRequired, moduleInstance); - } - } if (!isRequired) return createNonPresentModule(fullName, QStringLiteral("not found"), nullptr); - if (Q_UNLIKELY(triedToLoadModule)) + if (Q_UNLIKELY(triedToLoadModule)) { throw ErrorInfo(Tr::tr("Module %1 could not be loaded.").arg(fullName), dependsItemLocation); + } return nullptr; } |