AutoTest: Cache PCH lookups

Avoid slowing down scans of bigger projects by caching
lookups of paths inside precompiled headers.

Change-Id: I17652ba36ab761cc06ecce151e41253271236881
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2021-06-07 14:06:16 +02:00
parent 4245d89a67
commit 64ce2a6f43
2 changed files with 26 additions and 7 deletions

View File

@@ -35,6 +35,9 @@
namespace Autotest {
using LookupInfo = QPair<QString, QString>;
static QHash<LookupInfo, bool> s_pchLookupCache;
CppParser::CppParser(ITestFramework *framework)
: ITestParser(framework)
{
@@ -75,6 +78,7 @@ QByteArray CppParser::getFileContent(const Utils::FilePath &filePath) const
bool precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QString &cacheString,
const std::function<bool(const QString &)> &checker)
{
const CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
@@ -83,7 +87,14 @@ bool precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
return false;
const QStringList precompiledHeaders = projectParts.first()->precompiledHeaders;
auto headerContains = [&](const QString &header){
return Utils::anyOf(snapshot.allIncludesForDocument(header), checker);
LookupInfo info{header, cacheString};
auto it = s_pchLookupCache.find(info);
if (it == s_pchLookupCache.end()) {
it = s_pchLookupCache.insert(info,
Utils::anyOf(snapshot.allIncludesForDocument(header),
checker));
}
return it.value();
};
return Utils::anyOf(precompiledHeaders, headerContains);
}
@@ -92,24 +103,31 @@ bool CppParser::precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QString &headerFilePath)
{
return Autotest::precompiledHeaderContains(snapshot, filePath, [&](const QString &include) {
return include.endsWith(headerFilePath);
});
return Autotest::precompiledHeaderContains(snapshot,
filePath,
headerFilePath,
[&](const QString &include) {
return include.endsWith(headerFilePath);
});
}
bool CppParser::precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QRegularExpression &headerFileRegex)
{
return Autotest::precompiledHeaderContains(snapshot, filePath, [&](const QString &include) {
return headerFileRegex.match(include).hasMatch();
});
return Autotest::precompiledHeaderContains(snapshot,
filePath,
headerFileRegex.pattern(),
[&](const QString &include) {
return headerFileRegex.match(include).hasMatch();
});
}
void CppParser::release()
{
m_cppSnapshot = CPlusPlus::Snapshot();
m_workingCopy = CppTools::WorkingCopy();
s_pchLookupCache.clear();
}
CPlusPlus::Document::Ptr CppParser::document(const Utils::FilePath &fileName)

View File

@@ -95,6 +95,7 @@ public:
static bool precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot,
const Utils::FilePath &filePath,
const QRegularExpression &headerFileRegex);
protected:
CPlusPlus::Snapshot m_cppSnapshot;
CppTools::WorkingCopy m_workingCopy;