AutoTest: Improve handling of pch usage

Using a regular expression to search for possible macro usages
will not omit commented out lines.
So, check matches for being commented out to be able to filter
out false positives.

Change-Id: Ie8f982ef418a4538e87567354adb0eb54ea918de
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2021-09-23 12:29:54 +02:00
parent 82b250d901
commit 7b6f1000c6

View File

@@ -133,14 +133,29 @@ TestCases QtTestParser::testCases(const CppEditor::CppModelManager *modelManager
if (!astVisitor.testCases().isEmpty())
return astVisitor.testCases();
// check pch usage - might give false positives, but we can't do better without cost
TestCases result;
const QRegularExpression regex("\\bQTEST_(APPLESS_|GUILESS_)?MAIN"
const QRegularExpression regex("\\b(QTEST_(APPLESS_|GUILESS_)?MAIN)"
"\\s*\\(\\s*([[:alnum:]]+)\\s*\\)");
QRegularExpressionMatchIterator it = regex.globalMatch(QString::fromUtf8(fileContent));
QRegularExpressionMatchIterator it = regex.globalMatch(QString::fromUtf8(document->utf8Source()));
while (it.hasNext()) {
const QRegularExpressionMatch match = it.next();
result.append({match.captured(2), false});
const int start = match.capturedStart(1);
const int end = match.capturedEnd(1);
if (const auto *translationUnit = document->translationUnit()) {
bool commentedOut = false;
const int count = translationUnit->commentCount();
for (int curr = 0; curr < count; ++curr) {
CPlusPlus::Token token = translationUnit->commentAt(curr);
if (token.utf16charsBegin() <= start && token.utf16charsEnd() > end) {
commentedOut = true;
break;
}
}
if (commentedOut) // don't treat commented out macros as active
continue;
}
result.append({match.captured(3), false});
}
return result;
}