diff --git a/src/lib/utility/utility.h b/src/lib/utility/utility.h index 275f8300..ef7a49ce 100644 --- a/src/lib/utility/utility.h +++ b/src/lib/utility/utility.h @@ -145,13 +145,13 @@ std::vector> utility::splitToEqualySizedParts(const std::vector> parts; for (size_t i = 0; i < partCount; i++) { - parts.push_back(std::vector()); + parts.emplace_back(std::vector()); } int i = 0; for (const T& value : values) { - parts[i % partCount].push_back(value); + parts[i % partCount].emplace_back(value); ++i; } @@ -244,7 +244,7 @@ std::set utility::toSet(const std::vector& v) template void utility::fillVectorWithElements(std::vector& v, const T& arg) { - v.push_back(arg); + v.emplace_back(arg); } template @@ -269,7 +269,7 @@ std::vector utility::convert(const std::vector& sourceCo targetContainer.reserve(sourceContainer.size()); for (const SourceType& sourceElement : sourceContainer) { - targetContainer.push_back(conversion(sourceElement)); + targetContainer.emplace_back(conversion(sourceElement)); } return targetContainer; } @@ -281,7 +281,7 @@ std::vector utility::convert(const std::vector& sourceCo targetContainer.reserve(sourceContainer.size()); for (const SourceType& sourceElement : sourceContainer) { - targetContainer.push_back(TargetType(sourceElement)); + targetContainer.emplace_back(TargetType(sourceElement)); } return targetContainer; } diff --git a/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.cpp b/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.cpp index 8f0dc186..025769b8 100644 --- a/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.cpp +++ b/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.cpp @@ -12,51 +12,57 @@ void CxxIndexerCommandProvider::addCommand(const std::shared_ptr representation = std::make_shared(); - for (const FilePath& indexedPath : command->getIndexedPaths()) { - std::map::const_iterator it = m_indexedPathsToIds.find(indexedPath); - if (it != m_indexedPathsToIds.end()) + for (const FilePath& indexedPath : command->getIndexedPaths()) { - representation->m_indexedPathIds.insert(it->second); - } - else - { - const Id id = getId(); - m_indexedPathsToIds[indexedPath] = id; - m_idsToIndexedPaths[id] = indexedPath; - representation->m_indexedPathIds.insert(id); + std::map::const_iterator it = m_indexedPathsToIds.find(indexedPath); + if (it != m_indexedPathsToIds.end()) + { + representation->m_indexedPathIds.emplace(it->second); + } + else + { + const Id id = getId(); + m_indexedPathsToIds[indexedPath] = id; + m_idsToIndexedPaths[id] = indexedPath; + representation->m_indexedPathIds.emplace(id); + } } } - for (const FilePathFilter& excludeFilter : command->getExcludeFilters()) { - std::map::const_iterator it = m_excludeFiltersToIds.find(excludeFilter.wstr()); - if (it != m_excludeFiltersToIds.end()) + for (const FilePathFilter& excludeFilter : command->getExcludeFilters()) { - representation->m_excludeFilterIds.insert(it->second); - } - else - { - const Id id = getId(); - m_excludeFiltersToIds[excludeFilter.wstr()] = id; - m_idsToExcludeFilters[id] = excludeFilter.wstr(); - representation->m_excludeFilterIds.insert(id); + std::map::const_iterator it = m_excludeFiltersToIds.find(excludeFilter.wstr()); + if (it != m_excludeFiltersToIds.end()) + { + representation->m_excludeFilterIds.emplace(it->second); + } + else + { + const Id id = getId(); + m_excludeFiltersToIds[excludeFilter.wstr()] = id; + m_idsToExcludeFilters[id] = excludeFilter.wstr(); + representation->m_excludeFilterIds.emplace(id); + } } } - for (const FilePathFilter& includeFilter : command->getIncludeFilters()) { - std::map::const_iterator it = m_includeFiltersToIds.find(includeFilter.wstr()); - if (it != m_includeFiltersToIds.end()) + for (const FilePathFilter& includeFilter : command->getIncludeFilters()) { - representation->m_includeFilterIds.insert(it->second); - } - else - { - const Id id = getId(); - m_includeFiltersToIds[includeFilter.wstr()] = id; - m_idsToIncludeFilters[id] = includeFilter.wstr(); - representation->m_includeFilterIds.insert(id); + std::map::const_iterator it = m_includeFiltersToIds.find(includeFilter.wstr()); + if (it != m_includeFiltersToIds.end()) + { + representation->m_includeFilterIds.emplace(it->second); + } + else + { + const Id id = getId(); + m_includeFiltersToIds[includeFilter.wstr()] = id; + m_idsToIncludeFilters[id] = includeFilter.wstr(); + representation->m_includeFilterIds.emplace(id); + } } } @@ -76,51 +82,63 @@ void CxxIndexerCommandProvider::addCommand(const std::shared_ptrgetCompilerFlags()) { - std::map::const_iterator it = m_compilerFlagsToIds.find(compilerFlag); - if (it != m_compilerFlagsToIds.end()) + const std::vector& compilerFlags = command->getCompilerFlags(); + representation->m_compilerFlagIds.reserve(compilerFlags.size()); + for (const std::wstring& compilerFlag : compilerFlags) { - representation->m_compilerFlagIds.push_back(it->second); - } - else - { - const Id id = getId(); - m_compilerFlagsToIds[compilerFlag] = id; - m_idsToCompilerFlags[id] = compilerFlag; - representation->m_compilerFlagIds.push_back(id); + std::unordered_map::const_iterator it = m_compilerFlagsToIds.find(compilerFlag); + if (it != m_compilerFlagsToIds.end()) + { + representation->m_compilerFlagIds.emplace_back(it->second); + } + else + { + const Id id = getId(); + m_compilerFlagsToIds.emplace(compilerFlag, id); + m_idsToCompilerFlags.emplace(id, compilerFlag); + representation->m_compilerFlagIds.emplace_back(id); + } } } - for (const FilePath& systemHeaderSearchPath : command->getSystemHeaderSearchPaths()) { - std::map::const_iterator it = m_systemHeaderSearchPathsToIds.find(systemHeaderSearchPath); - if (it != m_systemHeaderSearchPathsToIds.end()) + const std::vector& systemHeaderSearchPaths = command->getSystemHeaderSearchPaths(); + representation->m_systemHeaderSearchPathIds.reserve(systemHeaderSearchPaths.size()); + for (const FilePath& systemHeaderSearchPath : systemHeaderSearchPaths) { - representation->m_systemHeaderSearchPathIds.push_back(it->second); - } - else - { - const Id id = getId(); - m_systemHeaderSearchPathsToIds[systemHeaderSearchPath] = id; - m_idsToSystemHeaderSearchPaths[id] = systemHeaderSearchPath; - representation->m_systemHeaderSearchPathIds.push_back(id); + std::map::const_iterator it = m_systemHeaderSearchPathsToIds.find(systemHeaderSearchPath); + if (it != m_systemHeaderSearchPathsToIds.end()) + { + representation->m_systemHeaderSearchPathIds.emplace_back(it->second); + } + else + { + const Id id = getId(); + m_systemHeaderSearchPathsToIds[systemHeaderSearchPath] = id; + m_idsToSystemHeaderSearchPaths[id] = systemHeaderSearchPath; + representation->m_systemHeaderSearchPathIds.emplace_back(id); + } } } - for (const FilePath& frameworkSearchPath : command->getFrameworkSearchPaths()) { - std::map::const_iterator it = m_frameworkSearchPathsToIds.find(frameworkSearchPath); - if (it != m_frameworkSearchPathsToIds.end()) + const std::vector& frameworkSearchPaths = command->getFrameworkSearchPaths(); + representation->m_frameworkSearchPathIds.reserve(frameworkSearchPaths.size()); + for (const FilePath& frameworkSearchPath : frameworkSearchPaths) { - representation->m_frameworkSearchPathIds.push_back(it->second); - } - else - { - const Id id = getId(); - m_frameworkSearchPathsToIds[frameworkSearchPath] = id; - m_idsToFrameworkSearchPaths[id] = frameworkSearchPath; - representation->m_frameworkSearchPathIds.push_back(id); + std::map::const_iterator it = m_frameworkSearchPathsToIds.find(frameworkSearchPath); + if (it != m_frameworkSearchPathsToIds.end()) + { + representation->m_frameworkSearchPathIds.emplace_back(it->second); + } + else + { + const Id id = getId(); + m_frameworkSearchPathsToIds[frameworkSearchPath] = id; + m_idsToFrameworkSearchPaths[id] = frameworkSearchPath; + representation->m_frameworkSearchPathIds.emplace_back(id); + } } } diff --git a/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.h b/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.h index 8bf14791..076e1020 100644 --- a/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.h +++ b/src/lib_cxx/data/indexer/CxxIndexerCommandProvider.h @@ -3,6 +3,7 @@ #include #include +#include #include "data/indexer/IndexerCommandProvider.h" #include "utility/types.h" @@ -54,7 +55,7 @@ private: std::map m_idsToFrameworkSearchPaths; std::map m_frameworkSearchPathsToIds; std::map m_idsToCompilerFlags; - std::map m_compilerFlagsToIds; + std::unordered_map m_compilerFlagsToIds; }; #endif // CXX_INDEXER_COMMAND_PROVIDER_H diff --git a/src/lib_cxx/project/SourceGroupCxxCdb.cpp b/src/lib_cxx/project/SourceGroupCxxCdb.cpp index ed01046e..d232ffe6 100644 --- a/src/lib_cxx/project/SourceGroupCxxCdb.cpp +++ b/src/lib_cxx/project/SourceGroupCxxCdb.cpp @@ -102,9 +102,8 @@ std::shared_ptr SourceGroupCxxCdb::getIndexerCommandProv const std::set excludeFilters = utility::toSet(m_settings->getExcludeFiltersExpandedAndAbsolute()); std::string error; - std::shared_ptr cdb = - std::shared_ptr( - clang::tooling::JSONCompilationDatabase::loadFromFile(utility::encodeToUtf8(cdbPath.wstr()), + std::shared_ptr cdb = std::shared_ptr( + clang::tooling::JSONCompilationDatabase::loadFromFile(utility::encodeToUtf8(cdbPath.wstr()), error, clang::tooling::JSONCommandLineSyntax::AutoDetect ) @@ -118,31 +117,38 @@ std::shared_ptr SourceGroupCxxCdb::getIndexerCommandProv } const std::set& sourceFilePaths = getAllSourceFilePaths(); - - for (const clang::tooling::CompileCommand& command: cdb->getAllCompileCommands()) + if (cdb) { - FilePath sourcePath = FilePath(utility::decodeFromUtf8(command.Filename)).makeCanonical(); - if (!sourcePath.isAbsolute()) + for (const clang::tooling::CompileCommand& command: cdb->getAllCompileCommands()) { - sourcePath = FilePath(utility::decodeFromUtf8(command.Directory + '/' + command.Filename)).makeCanonical(); - } + FilePath sourcePath = FilePath(utility::decodeFromUtf8(command.Filename)).makeCanonical(); + if (!sourcePath.isAbsolute()) + { + sourcePath = FilePath(utility::decodeFromUtf8(command.Directory + '/' + command.Filename)).makeCanonical(); + } - if (filesToIndex.find(sourcePath) != filesToIndex.end() && - sourceFilePaths.find(sourcePath) != sourceFilePaths.end()) - { - provider->addCommand(std::make_shared( - sourcePath, - utility::concat(indexedHeaderPaths, { sourcePath }), - excludeFilters, - std::set(), - FilePath(utility::decodeFromUtf8(command.Directory)), - systemHeaderSearchPaths, - frameworkSearchPaths, - utility::concat( - utility::convert(command.CommandLine, [](const std::string& arg) { return utility::decodeFromUtf8(arg); }), - compilerFlags - ) - )); + if (filesToIndex.find(sourcePath) != filesToIndex.end() && + sourceFilePaths.find(sourcePath) != sourceFilePaths.end()) + { + std::vector mergedCompilerFlags; + mergedCompilerFlags.reserve(compilerFlags.size() + command.CommandLine.size()); + for (const std::string& arg : command.CommandLine) + { + mergedCompilerFlags.emplace_back(utility::decodeFromUtf8(arg)); + } + mergedCompilerFlags.insert(mergedCompilerFlags.end(), compilerFlags.begin(), compilerFlags.end()); + + provider->addCommand(std::make_shared( + sourcePath, + utility::concat(indexedHeaderPaths, { sourcePath }), + excludeFilters, + std::set(), + FilePath(utility::decodeFromUtf8(command.Directory)), + systemHeaderSearchPaths, + frameworkSearchPaths, + mergedCompilerFlags + )); + } } } }