Skip to content

Commit 5109454

Browse files
authoredOct 19, 2023
[clang][index] Fix processing of CompoundAssignOperator at setting up reference roles (#69370)
Without this patch in expressions like `foo += 1` reference `foo` has no read and write roles. This happens because `CompoundAssignOperator` is also a `BinaryOperator`, thus handling `CompoindAssignOperator` in `else` branch is a dead code.
1 parent 616c86a commit 5109454

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed
 

‎clang/lib/Index/IndexBody.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,15 @@ class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
7777
const Stmt *Parent = *It;
7878

7979
if (auto BO = dyn_cast<BinaryOperator>(Parent)) {
80-
if (BO->getOpcode() == BO_Assign && BO->getLHS()->IgnoreParenCasts() == E)
81-
Roles |= (unsigned)SymbolRole::Write;
82-
80+
if (BO->getOpcode() == BO_Assign) {
81+
if (BO->getLHS()->IgnoreParenCasts() == E)
82+
Roles |= (unsigned)SymbolRole::Write;
83+
} else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) {
84+
if (CA->getLHS()->IgnoreParenCasts() == E) {
85+
Roles |= (unsigned)SymbolRole::Read;
86+
Roles |= (unsigned)SymbolRole::Write;
87+
}
88+
}
8389
} else if (auto UO = dyn_cast<UnaryOperator>(Parent)) {
8490
if (UO->isIncrementDecrementOp()) {
8591
Roles |= (unsigned)SymbolRole::Read;
@@ -88,12 +94,6 @@ class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
8894
Roles |= (unsigned)SymbolRole::AddressOf;
8995
}
9096

91-
} else if (auto CA = dyn_cast<CompoundAssignOperator>(Parent)) {
92-
if (CA->getLHS()->IgnoreParenCasts() == E) {
93-
Roles |= (unsigned)SymbolRole::Read;
94-
Roles |= (unsigned)SymbolRole::Write;
95-
}
96-
9797
} else if (auto CE = dyn_cast<CallExpr>(Parent)) {
9898
if (CE->getCallee()->IgnoreParenCasts() == E) {
9999
addCallRole(Roles, Relations);

‎clang/unittests/Index/IndexTests.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,31 @@ TEST(IndexTest, NonTypeTemplateParameter) {
428428
WrittenAt(Position(3, 15)))));
429429
}
430430

431+
TEST(IndexTest, ReadWriteRoles) {
432+
std::string Code = R"cpp(
433+
int main() {
434+
int foo = 0;
435+
foo = 2;
436+
foo += 1;
437+
int bar = foo;
438+
}
439+
)cpp";
440+
auto Index = std::make_shared<Indexer>();
441+
IndexingOptions Opts;
442+
Opts.IndexFunctionLocals = true;
443+
tooling::runToolOnCode(std::make_unique<IndexAction>(Index, Opts), Code);
444+
EXPECT_THAT(
445+
Index->Symbols,
446+
AllOf(Contains(AllOf(QName("foo"), HasRole(SymbolRole::Write),
447+
WrittenAt(Position(4, 7)))),
448+
Contains(AllOf(QName("foo"),
449+
HasRole(static_cast<unsigned>(SymbolRole::Read) |
450+
static_cast<unsigned>(SymbolRole::Write)),
451+
WrittenAt(Position(5, 7)))),
452+
Contains(AllOf(QName("foo"), HasRole(SymbolRole::Read),
453+
WrittenAt(Position(6, 17))))));
454+
}
455+
431456
} // namespace
432457
} // namespace index
433458
} // namespace clang

0 commit comments

Comments
 (0)