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;
|
QList<MessageId> pendingAstRequests;
|
||||||
QPointer<SearchResult> search;
|
QPointer<SearchResult> search;
|
||||||
std::optional<ReplacementData> replacementData;
|
std::optional<ReplacementData> replacementData;
|
||||||
|
QString searchTerm;
|
||||||
bool canceled = false;
|
bool canceled = false;
|
||||||
bool categorize = false;
|
bool categorize = false;
|
||||||
};
|
};
|
||||||
@@ -85,6 +86,7 @@ ClangdFindReferences::ClangdFindReferences(ClangdClient *client, TextDocument *d
|
|||||||
: QObject(client), d(new ClangdFindReferences::Private(this))
|
: QObject(client), d(new ClangdFindReferences::Private(this))
|
||||||
{
|
{
|
||||||
d->categorize = categorize;
|
d->categorize = categorize;
|
||||||
|
d->searchTerm = searchTerm;
|
||||||
if (replacement) {
|
if (replacement) {
|
||||||
ReplacementData replacementData;
|
ReplacementData replacementData;
|
||||||
replacementData.oldSymbolName = searchTerm;
|
replacementData.oldSymbolName = searchTerm;
|
||||||
@@ -286,7 +288,7 @@ void ClangdFindReferences::Private::reportAllSearchResultsAndFinish()
|
|||||||
finishSearch();
|
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,
|
void ClangdFindReferences::Private::addSearchResultsForFile(const FilePath &file,
|
||||||
const ReferencesFileData &fileData)
|
const ReferencesFileData &fileData)
|
||||||
@@ -296,7 +298,7 @@ void ClangdFindReferences::Private::addSearchResultsForFile(const FilePath &file
|
|||||||
for (const auto &rangeWithText : fileData.rangesAndLineText) {
|
for (const auto &rangeWithText : fileData.rangesAndLineText) {
|
||||||
const Range &range = rangeWithText.first;
|
const Range &range = rangeWithText.first;
|
||||||
const ClangdAstPath astPath = getAstPath(fileData.ast, range);
|
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();
|
: Usage::Tags();
|
||||||
|
|
||||||
SearchResultItem item;
|
SearchResultItem item;
|
||||||
@@ -349,11 +351,14 @@ std::optional<QString> ClangdFindReferences::Private::getContainingFunctionName(
|
|||||||
return containingFuncNode->detail();
|
return containingFuncNode->detail();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Usage::Tags getUsageType(const ClangdAstPath &path)
|
static Usage::Tags getUsageType(const ClangdAstPath &path, const QString &searchTerm)
|
||||||
{
|
{
|
||||||
bool potentialWrite = false;
|
bool potentialWrite = false;
|
||||||
bool isFunction = false;
|
bool isFunction = false;
|
||||||
const bool symbolIsDataType = path.last().role() == "type" && path.last().kind() == "Record";
|
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; };
|
const auto isPotentialWrite = [&] { return potentialWrite && !isFunction; };
|
||||||
for (auto pathIt = path.rbegin(); pathIt != path.rend(); ++pathIt) {
|
for (auto pathIt = path.rbegin(); pathIt != path.rend(); ++pathIt) {
|
||||||
if (pathIt->arcanaContains("non_odr_use_unevaluated"))
|
if (pathIt->arcanaContains("non_odr_use_unevaluated"))
|
||||||
@@ -386,6 +391,8 @@ static Usage::Tags getUsageType(const ClangdAstPath &path)
|
|||||||
if (pathIt->role() == "declaration") {
|
if (pathIt->role() == "declaration") {
|
||||||
if (symbolIsDataType)
|
if (symbolIsDataType)
|
||||||
return {};
|
return {};
|
||||||
|
if (!invokedConstructor.isEmpty() && invokedConstructor == searchTerm)
|
||||||
|
return {};
|
||||||
if (pathIt->arcanaContains("cinit")) {
|
if (pathIt->arcanaContains("cinit")) {
|
||||||
if (pathIt == path.rbegin())
|
if (pathIt == path.rbegin())
|
||||||
return {Usage::Tag::Declaration, Usage::Tag::Write};
|
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(58, 10, Usage::Tag::Read), makeItem(58, 22, Usage::Tag::Read),
|
||||||
makeItem(59, 4, Usage::Tag::Write), makeItem(59, 21, 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{
|
QTest::newRow("subclass") << "defs.h" << 450 << ItemList{
|
||||||
makeItem(20, 7, Usage::Tag::Declaration), makeItem(5, 4, Usage::Tags()),
|
makeItem(20, 7, Usage::Tag::Declaration), makeItem(5, 4, Usage::Tags()),
|
||||||
makeItem(13, 21, Usage::Tags()), makeItem(32, 8, Usage::Tags())};
|
makeItem(13, 21, Usage::Tags()), makeItem(32, 8, Usage::Tags())};
|
||||||
|
|||||||
@@ -26,3 +26,12 @@ void func2(const int &);
|
|||||||
void func3(int *);
|
void func3(int *);
|
||||||
void func4(const int *);
|
void func4(const int *);
|
||||||
void func5(int);
|
void func5(int);
|
||||||
|
|
||||||
|
struct S3 {
|
||||||
|
S3();
|
||||||
|
};
|
||||||
|
|
||||||
|
void s3Use()
|
||||||
|
{
|
||||||
|
S3 s3;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user