forked from qt-creator/qt-creator
ClangCodeModel: Fix usage type of constructors in "Find References"
In particular, in a variable definition, the (invisible) reference to the constructor is *not* a declaration (but an implicit call). Change-Id: Ic1f29a4da360959e81ec536efbf1175924ea34d7 Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -75,6 +75,7 @@ public:
|
||||
QList<MessageId> pendingAstRequests;
|
||||
QPointer<SearchResult> search;
|
||||
std::optional<ReplacementData> replacementData;
|
||||
QString searchTerm;
|
||||
bool canceled = false;
|
||||
bool categorize = false;
|
||||
};
|
||||
@@ -85,6 +86,7 @@ ClangdFindReferences::ClangdFindReferences(ClangdClient *client, TextDocument *d
|
||||
: QObject(client), d(new ClangdFindReferences::Private(this))
|
||||
{
|
||||
d->categorize = categorize;
|
||||
d->searchTerm = searchTerm;
|
||||
if (replacement) {
|
||||
ReplacementData replacementData;
|
||||
replacementData.oldSymbolName = searchTerm;
|
||||
@@ -286,7 +288,7 @@ void ClangdFindReferences::Private::reportAllSearchResultsAndFinish()
|
||||
finishSearch();
|
||||
}
|
||||
|
||||
static Usage::Tags getUsageType(const ClangdAstPath &path);
|
||||
static Usage::Tags getUsageType(const ClangdAstPath &path, const QString &searchTerm);
|
||||
|
||||
void ClangdFindReferences::Private::addSearchResultsForFile(const FilePath &file,
|
||||
const ReferencesFileData &fileData)
|
||||
@@ -296,7 +298,7 @@ void ClangdFindReferences::Private::addSearchResultsForFile(const FilePath &file
|
||||
for (const auto &rangeWithText : fileData.rangesAndLineText) {
|
||||
const Range &range = rangeWithText.first;
|
||||
const ClangdAstPath astPath = getAstPath(fileData.ast, range);
|
||||
const Usage::Tags usageType = fileData.ast.isValid() ? getUsageType(astPath)
|
||||
const Usage::Tags usageType = fileData.ast.isValid() ? getUsageType(astPath, searchTerm)
|
||||
: Usage::Tags();
|
||||
|
||||
SearchResultItem item;
|
||||
@@ -349,11 +351,14 @@ std::optional<QString> ClangdFindReferences::Private::getContainingFunctionName(
|
||||
return containingFuncNode->detail();
|
||||
}
|
||||
|
||||
static Usage::Tags getUsageType(const ClangdAstPath &path)
|
||||
static Usage::Tags getUsageType(const ClangdAstPath &path, const QString &searchTerm)
|
||||
{
|
||||
bool potentialWrite = false;
|
||||
bool isFunction = false;
|
||||
const bool symbolIsDataType = path.last().role() == "type" && path.last().kind() == "Record";
|
||||
QString invokedConstructor;
|
||||
if (path.last().role() == "expression" && path.last().kind() == "CXXConstruct")
|
||||
invokedConstructor = path.last().detail().value_or(QString());
|
||||
const auto isPotentialWrite = [&] { return potentialWrite && !isFunction; };
|
||||
for (auto pathIt = path.rbegin(); pathIt != path.rend(); ++pathIt) {
|
||||
if (pathIt->arcanaContains("non_odr_use_unevaluated"))
|
||||
@@ -386,6 +391,8 @@ static Usage::Tags getUsageType(const ClangdAstPath &path)
|
||||
if (pathIt->role() == "declaration") {
|
||||
if (symbolIsDataType)
|
||||
return {};
|
||||
if (!invokedConstructor.isEmpty() && invokedConstructor == searchTerm)
|
||||
return {};
|
||||
if (pathIt->arcanaContains("cinit")) {
|
||||
if (pathIt == path.rbegin())
|
||||
return {Usage::Tag::Declaration, Usage::Tag::Write};
|
||||
|
||||
@@ -261,6 +261,13 @@ void ClangdTestFindReferences::test_data()
|
||||
makeItem(58, 10, Usage::Tag::Read), makeItem(58, 22, Usage::Tag::Read),
|
||||
makeItem(59, 4, Usage::Tag::Write), makeItem(59, 21, Usage::Tag::Read)};
|
||||
|
||||
QTest::newRow("struct type 2") << "defs.h" << 450 << ItemList{
|
||||
makeItem(20, 7, Usage::Tag::Declaration), makeItem(5, 4, Usage::Tags()),
|
||||
makeItem(13, 21, Usage::Tags()), makeItem(32, 8, Usage::Tags())};
|
||||
|
||||
QTest::newRow("constructor") << "defs.h" << 627 << ItemList{
|
||||
makeItem(31, 4, Usage::Tag::Declaration), makeItem(36, 7, Usage::Tags())};
|
||||
|
||||
QTest::newRow("subclass") << "defs.h" << 450 << ItemList{
|
||||
makeItem(20, 7, Usage::Tag::Declaration), makeItem(5, 4, Usage::Tags()),
|
||||
makeItem(13, 21, Usage::Tags()), makeItem(32, 8, Usage::Tags())};
|
||||
|
||||
@@ -26,3 +26,12 @@ void func2(const int &);
|
||||
void func3(int *);
|
||||
void func4(const int *);
|
||||
void func5(int);
|
||||
|
||||
struct S3 {
|
||||
S3();
|
||||
};
|
||||
|
||||
void s3Use()
|
||||
{
|
||||
S3 s3;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user