diff --git a/src/plugins/autotest/itestparser.cpp b/src/plugins/autotest/itestparser.cpp index 99b760f78bd..9438fe4caf1 100644 --- a/src/plugins/autotest/itestparser.cpp +++ b/src/plugins/autotest/itestparser.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -105,6 +106,31 @@ bool CppParser::precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot, }); } +std::optional> CppParser::filesContainingMacro(const QByteArray ¯oName) +{ + // safety net to avoid adding some option + static const bool noPrefilter = qtcEnvironmentVariableIsSet("QTC_AUTOTEST_DISABLE_PREFILTER"); + if (noPrefilter) + return std::nullopt; + + QSet result; + CppEditor::ProjectInfo::ConstPtr infos = CppEditor::CppModelManager::projectInfo( + ProjectExplorer::ProjectManager::startupProject()); + if (!infos) + return std::nullopt; + + const auto projectParts = infos->projectParts(); + for (const auto &pp : projectParts) { + if (!pp->selectedForBuilding) + continue; + + const ProjectExplorer::Macros macros = pp->projectMacros; + if (Utils::anyOf(pp->projectMacros, Utils::equal(&ProjectExplorer::Macro::key, macroName))) + result.unite(Utils::transform(pp->files, &CppEditor::ProjectFile::path)); + } + return std::make_optional(result); +} + void CppParser::release() { m_cppSnapshot = CPlusPlus::Snapshot(); diff --git a/src/plugins/autotest/itestparser.h b/src/plugins/autotest/itestparser.h index 93233bd7a97..6e3305dd6ea 100644 --- a/src/plugins/autotest/itestparser.h +++ b/src/plugins/autotest/itestparser.h @@ -9,6 +9,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE template class QPromise; @@ -76,6 +78,9 @@ public: static bool precompiledHeaderContains(const CPlusPlus::Snapshot &snapshot, const Utils::FilePath &filePath, const QRegularExpression &headerFileRegex); + // returns all files of the startup project whose ProjectPart has the given \a macroName + // set as a project define + static std::optional> filesContainingMacro(const QByteArray ¯oName); protected: CPlusPlus::Snapshot m_cppSnapshot; diff --git a/src/plugins/autotest/qtest/qttestparser.cpp b/src/plugins/autotest/qtest/qttestparser.cpp index 824f4860a45..a74944b7b64 100644 --- a/src/plugins/autotest/qtest/qttestparser.cpp +++ b/src/plugins/autotest/qtest/qttestparser.cpp @@ -295,6 +295,9 @@ static bool isQObject(const CPlusPlus::Document::Ptr &declaringDoc) bool QtTestParser::processDocument(QPromise &promise, const FilePath &fileName) { + if (!m_prefilteredFiles.contains(fileName)) + return false; + CPlusPlus::Document::Ptr doc = document(fileName); if (doc.isNull()) return false; @@ -418,6 +421,12 @@ void QtTestParser::init(const QSet &filesToParse, bool fullParse) m_testCases = QTestUtils::testCaseNamesForFiles(framework(), filesToParse); m_alternativeFiles = QTestUtils::alternativeFiles(framework(), filesToParse); } + + if (std::optional> prefiltered = filesContainingMacro("QT_TESTLIB_LIB")) + m_prefilteredFiles = prefiltered->intersect(filesToParse); + else + m_prefilteredFiles = filesToParse; + CppParser::init(filesToParse, fullParse); } @@ -425,6 +434,7 @@ void QtTestParser::release() { m_testCases.clear(); m_alternativeFiles.clear(); + m_prefilteredFiles.clear(); CppParser::release(); } diff --git a/src/plugins/autotest/qtest/qttestparser.h b/src/plugins/autotest/qtest/qttestparser.h index ece2bc639c5..9f8c44593f0 100644 --- a/src/plugins/autotest/qtest/qttestparser.h +++ b/src/plugins/autotest/qtest/qttestparser.h @@ -57,6 +57,7 @@ private: const QString &projectFile) const; QHash m_testCases; QMultiHash m_alternativeFiles; + QSet m_prefilteredFiles; }; } // namespace Internal diff --git a/src/plugins/autotest/quick/quicktestparser.cpp b/src/plugins/autotest/quick/quicktestparser.cpp index eaba2299588..ef0bba8cc89 100644 --- a/src/plugins/autotest/quick/quicktestparser.cpp +++ b/src/plugins/autotest/quick/quicktestparser.cpp @@ -28,8 +28,7 @@ using namespace QmlJS; using namespace Utils; -namespace Autotest { -namespace Internal { +namespace Autotest::Internal { TestTreeItem *QuickTestParseResult::createTestTreeItem() const { @@ -359,6 +358,11 @@ void QuickTestParser::init(const QSet &filesToParse, bool fullParse) m_checkForDerivedTests = theQtTestFramework().quickCheckForDerivedTests(); + if (std::optional> prefiltered = filesContainingMacro("QT_QMLTEST_LIB")) + m_prefilteredFiles = prefiltered->intersect(filesToParse); + else + m_prefilteredFiles = filesToParse; + CppParser::init(filesToParse, fullParse); } @@ -366,6 +370,7 @@ void QuickTestParser::release() { m_qmlSnapshot = Snapshot(); m_proFilesForQmlFiles.clear(); + m_prefilteredFiles.clear(); CppParser::release(); } @@ -384,6 +389,9 @@ bool QuickTestParser::processDocument(QPromise &promise, m_checkForDerivedTests); } + if (!m_prefilteredFiles.contains(fileName)) + return false; + CPlusPlus::Document::Ptr cppdoc = document(fileName); if (cppdoc.isNull() || !includesQtQuickTest(cppdoc, m_cppSnapshot)) return false; @@ -396,5 +404,4 @@ FilePath QuickTestParser::projectFileForMainCppFile(const FilePath &fileName) co return m_mainCppFiles.contains(fileName) ? m_mainCppFiles.value(fileName) : FilePath(); } -} // namespace Internal -} // namespace Autotest +} // namespace Autotest::Internal diff --git a/src/plugins/autotest/quick/quicktestparser.h b/src/plugins/autotest/quick/quicktestparser.h index dfd71333ead..e93fa9605f4 100644 --- a/src/plugins/autotest/quick/quicktestparser.h +++ b/src/plugins/autotest/quick/quicktestparser.h @@ -44,6 +44,7 @@ private: QFileSystemWatcher m_directoryWatcher; QMap > m_watchedFiles; QMap m_mainCppFiles; + QSet m_prefilteredFiles; bool m_checkForDerivedTests = false; };