Skip to content

[clang][index] Fix processing of CompoundAssignOperator at setting up reference roles #69370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions clang/lib/Index/IndexBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
const Stmt *Parent = *It;

if (auto BO = dyn_cast<BinaryOperator>(Parent)) {
if (BO->getOpcode() == BO_Assign && BO->getLHS()->IgnoreParenCasts() == E)
Roles |= (unsigned)SymbolRole::Write;

if (BO->getOpcode() == BO_Assign) {
if (BO->getLHS()->IgnoreParenCasts() == E)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consider inlining this if to the one above (like the original code) to the code less nested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split this if to avoid this check else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) if BO->getOpcode() == BO_Assign && BO->getLHS()->IgnoreParenCasts() != E

I.e. if BO->getOpcode() == BO_Assign then Parent is not a CompoundAssignOperator and we don't need to check this

Roles |= (unsigned)SymbolRole::Write;
} else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) {
if (CA->getLHS()->IgnoreParenCasts() == E) {
Roles |= (unsigned)SymbolRole::Read;
Roles |= (unsigned)SymbolRole::Write;
}
}
} else if (auto UO = dyn_cast<UnaryOperator>(Parent)) {
if (UO->isIncrementDecrementOp()) {
Roles |= (unsigned)SymbolRole::Read;
Expand All @@ -88,12 +94,6 @@ class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
Roles |= (unsigned)SymbolRole::AddressOf;
}

} else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) {
if (CA->getLHS()->IgnoreParenCasts() == E) {
Roles |= (unsigned)SymbolRole::Read;
Roles |= (unsigned)SymbolRole::Write;
}

} else if (auto CE = dyn_cast<CallExpr>(Parent)) {
if (CE->getCallee()->IgnoreParenCasts() == E) {
addCallRole(Roles, Relations);
Expand Down
25 changes: 25 additions & 0 deletions clang/unittests/Index/IndexTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,31 @@ TEST(IndexTest, NonTypeTemplateParameter) {
WrittenAt(Position(3, 15)))));
}

TEST(IndexTest, ReadWriteRoles) {
std::string Code = R"cpp(
int main() {
int foo = 0;
foo = 2;
foo += 1;
int bar = foo;
}
)cpp";
auto Index = std::make_shared<Indexer>();
IndexingOptions Opts;
Opts.IndexFunctionLocals = true;
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
EXPECT_THAT(
Index->Symbols,
AllOf(Contains(AllOf(QName("foo"), HasRole(SymbolRole::Write),
WrittenAt(Position(4, 7)))),
Contains(AllOf(QName("foo"),
HasRole(static_cast<unsigned>(SymbolRole::Read) |
static_cast<unsigned>(SymbolRole::Write)),
WrittenAt(Position(5, 7)))),
Contains(AllOf(QName("foo"), HasRole(SymbolRole::Read),
WrittenAt(Position(6, 17))))));
}

} // namespace
} // namespace index
} // namespace clang