ClangCodeModel: Fix mis-detection of class members as operators

The name check was not tight enough.

Change-Id: I5d813a29525bd5b5c23ce04f0bd9e5982a36536e
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2022-04-20 17:27:51 +02:00
parent 2529b62315
commit cf96a91b69
3 changed files with 28 additions and 3 deletions

View File

@@ -3839,10 +3839,18 @@ void ExtraHighlightingResultsCollector::collectFromNode(const AstNode &node)
QString detail = node.detail().value_or(QString()); QString detail = node.detail().value_or(QString());
const bool isCallToNew = node.kind() == "CXXNew"; const bool isCallToNew = node.kind() == "CXXNew";
const bool isCallToDelete = node.kind() == "CXXDelete"; const bool isCallToDelete = node.kind() == "CXXDelete";
if (!isCallToNew && !isCallToDelete const auto isProperOperator = [&] {
&& (!detail.startsWith(operatorPrefix) || detail == operatorPrefix)) { if (isCallToNew || isCallToDelete)
return true;
if (!detail.startsWith(operatorPrefix))
return false;
if (detail == operatorPrefix)
return false;
const QChar nextChar = detail.at(operatorPrefix.length());
return !nextChar.isLetterOrNumber() && nextChar != '_';
};
if (!isProperOperator())
return; return;
}
if (!isCallToNew && !isCallToDelete) if (!isCallToNew && !isCallToDelete)
detail.remove(0, operatorPrefix.length()); detail.remove(0, operatorPrefix.length());

View File

@@ -1329,6 +1329,14 @@ void ClangdTestHighlighting::test_data()
<< QList<int>{C_FIELD} << 0; << QList<int>{C_FIELD} << 0;
QTest::newRow("pass inherited member by value") << 1038 << 21 << 1038 << 26 QTest::newRow("pass inherited member by value") << 1038 << 21 << 1038 << 26
<< QList<int>{C_FIELD} << 0; << QList<int>{C_FIELD} << 0;
QTest::newRow("fake operator member declaration") << 1045 << 9 << 1045 << 23
<< QList<int>{C_FIELD, C_DECLARATION} << 0;
QTest::newRow("fake operator method declaration") << 1046 << 10 << 1046 << 24
<< QList<int>{C_FUNCTION, C_DECLARATION} << 0;
QTest::newRow("fake operator member access") << 1049 << 8 << 1049 << 22
<< QList<int>{C_FIELD} << 0;
QTest::newRow("fake operator method call") << 1050 << 8 << 1050 << 22
<< QList<int>{C_FUNCTION} << 0;
} }
void ClangdTestHighlighting::test() void ClangdTestHighlighting::test()

View File

@@ -1040,3 +1040,12 @@ template<typename T> class Derived2 : public BaseWithMember2
return false; return false;
} }
}; };
struct StructWithMisleadingMemberNames {
int operatormember;
void operatorMethod();
};
void useStrangeStruct(StructWithMisleadingMemberNames *s) {
s->operatormember = 5;
s->operatorMethod();
}