AutoTest: Use working copy of own parser

In TestCodeParser::syncTestFrameworks(), a parser is created for every
test framework. As a result, the last parser being created would "win"
the global s_parserInstance variable, which is not predictable and
probably not intended. So turn CppParser::getFileContent() into a non-
static method, avoiding the global variable altogether.

Change-Id: I9f7560f1185bc4a3bc7b2b36e89280351998465e
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Bernhard Beschow
2021-01-01 19:19:10 +01:00
parent d8c1e51bfe
commit 5d7f2d7444
9 changed files with 71 additions and 103 deletions

View File

@@ -25,7 +25,6 @@
#include "qttestparser.h"
#include "qttestframework.h"
#include "qttesttreeitem.h"
#include "qttestvisitors.h"
#include "qttest_utils.h"
@@ -94,10 +93,9 @@ static bool qtTestLibDefined(const QString &fileName)
return false;
}
static QString testClass(const CppTools::CppModelManager *modelManager,
const CPlusPlus::Snapshot &snapshot, const QString &fileName)
QString QtTestParser::testClass(const CppTools::CppModelManager *modelManager, const QString &fileName) const
{
const QByteArray &fileContent = CppParser::getFileContent(fileName);
const QByteArray &fileContent = getFileContent(fileName);
CPlusPlus::Document::Ptr document = modelManager->document(fileName);
if (document.isNull())
return QString();
@@ -115,10 +113,10 @@ static QString testClass(const CppTools::CppModelManager *modelManager,
}
}
// check if one has used a self-defined macro or QTest::qExec() directly
document = snapshot.preprocessedDocument(fileContent, fileName);
document = m_cppSnapshot.preprocessedDocument(fileContent, fileName);
document->check();
CPlusPlus::AST *ast = document->translationUnit()->ast();
TestAstVisitor astVisitor(document, snapshot);
TestAstVisitor astVisitor(document, m_cppSnapshot);
astVisitor.accept(ast);
return astVisitor.className();
}
@@ -181,11 +179,10 @@ static QSet<QString> filesWithDataFunctionDefinitions(
return result;
}
static QHash<QString, QtTestCodeLocationList> checkForDataTags(const QString &fileName,
const CPlusPlus::Snapshot &snapshot)
QHash<QString, QtTestCodeLocationList> QtTestParser::checkForDataTags(const QString &fileName) const
{
const QByteArray fileContent = CppParser::getFileContent(fileName);
CPlusPlus::Document::Ptr document = snapshot.preprocessedDocument(fileContent, fileName);
const QByteArray fileContent = getFileContent(fileName);
CPlusPlus::Document::Ptr document = m_cppSnapshot.preprocessedDocument(fileContent, fileName);
document->check();
CPlusPlus::AST *ast = document->translationUnit()->ast();
TestDataFunctionVisitor visitor(document);
@@ -280,28 +277,31 @@ static bool isQObject(const CPlusPlus::Document::Ptr &declaringDoc)
|| file.endsWith("QtCore/qobject.h") || file.endsWith("kernel/qobject.h");
}
static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
CPlusPlus::Document::Ptr document,
const CPlusPlus::Snapshot &snapshot,
const QString &oldTestCaseName,
const QStringList &alternativeFiles,
ITestBase *base)
bool QtTestParser::processDocument(QFutureInterface<TestParseResultPtr> futureInterface,
const QString &fileName)
{
CPlusPlus::Document::Ptr doc = document(fileName);
if (doc.isNull())
return false;
const QString &oldTestCaseName = m_testCaseNames.value(fileName);
if ((!includesQtTest(doc, m_cppSnapshot) || !qtTestLibDefined(fileName)) && oldTestCaseName.isEmpty())
return false;
const CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
const QString &fileName = document->fileName();
QString testCaseName(testClass(modelManager, snapshot, fileName));
QString testCaseName(testClass(modelManager, fileName));
// we might be in a reparse without the original entry point with the QTest::qExec()
if (testCaseName.isEmpty())
testCaseName = oldTestCaseName;
if (!testCaseName.isEmpty()) {
int line = 0;
int column = 0;
CPlusPlus::Document::Ptr declaringDoc = declaringDocument(document, snapshot, testCaseName,
const QStringList &alternativeFiles = m_alternativeFiles.values(fileName);
CPlusPlus::Document::Ptr declaringDoc = declaringDocument(doc, m_cppSnapshot, testCaseName,
alternativeFiles, &line, &column);
if (declaringDoc.isNull())
return false;
TestVisitor visitor(testCaseName, snapshot);
TestVisitor visitor(testCaseName, m_cppSnapshot);
visitor.accept(declaringDoc->globalNamespace());
if (!visitor.resultValid())
return false;
@@ -310,7 +310,7 @@ static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
// gather appropriate information of base classes as well and merge into already found
// functions - but only as far as QtTest can handle this appropriate
fetchAndMergeBaseTestFunctions(
visitor.baseClasses(), testFunctions, declaringDoc, snapshot);
visitor.baseClasses(), testFunctions, declaringDoc, m_cppSnapshot);
// handle tests that are not runnable without more information (plugin unit test of QC)
if (testFunctions.isEmpty() && testCaseName == "QObject" && isQObject(declaringDoc))
@@ -320,9 +320,9 @@ static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
QHash<QString, QtTestCodeLocationList> dataTags;
for (const QString &file : files)
Utils::addToHash(&dataTags, checkForDataTags(file, snapshot));
Utils::addToHash(&dataTags, checkForDataTags(file));
QtTestParseResult *parseResult = new QtTestParseResult(base);
QtTestParseResult *parseResult = new QtTestParseResult(framework());
parseResult->itemType = TestTreeItem::TestCase;
parseResult->fileName = declaringDoc->fileName();
parseResult->name = testCaseName;
@@ -339,7 +339,7 @@ static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
const QtTestCodeLocationAndType &location = it.value();
QString functionName = it.key();
functionName = functionName.mid(functionName.lastIndexOf(':') + 1);
QtTestParseResult *func = new QtTestParseResult(base);
QtTestParseResult *func = new QtTestParseResult(framework());
func->itemType = location.m_type;
func->name = testCaseName + "::" + functionName;
func->displayName = functionName;
@@ -350,7 +350,7 @@ static bool handleQtTest(QFutureInterface<TestParseResultPtr> futureInterface,
const QtTestCodeLocationList &tagLocations = tagLocationsFor(func, dataTags);
for (const QtTestCodeLocationAndType &tag : tagLocations) {
QtTestParseResult *dataTag = new QtTestParseResult(base);
QtTestParseResult *dataTag = new QtTestParseResult(framework());
dataTag->itemType = tag.m_type;
dataTag->name = tag.m_name;
dataTag->displayName = tag.m_name;
@@ -386,19 +386,5 @@ void QtTestParser::release()
CppParser::release();
}
bool QtTestParser::processDocument(QFutureInterface<TestParseResultPtr> futureInterface,
const QString &fileName)
{
CPlusPlus::Document::Ptr doc = document(fileName);
if (doc.isNull())
return false;
const QString &oldName = m_testCaseNames.value(fileName);
const QStringList &alternativeFiles = m_alternativeFiles.values(fileName);
if ((!includesQtTest(doc, m_cppSnapshot) || !qtTestLibDefined(fileName)) && oldName.isEmpty())
return false;
return handleQtTest(futureInterface, doc, m_cppSnapshot, oldName, alternativeFiles, framework());
}
} // namespace Internal
} // namespace Autotest