Skip to content

Commit bbf234b

Browse files
committed
[CodeCompletion] Complete designators for fields in anonymous structs/unions
Fixes clangd/clangd#836 Differential Revision: https://reviews.llvm.org/D116717
1 parent ed7ae1a commit bbf234b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6344,7 +6344,15 @@ void Sema::CodeCompleteDesignator(QualType BaseType,
63446344
CodeCompleter->getCodeCompletionTUInfo(), CCC);
63456345

63466346
Results.EnterNewScope();
6347-
for (const auto *FD : RD->fields()) {
6347+
for (const Decl *D : RD->decls()) {
6348+
const FieldDecl *FD;
6349+
if (auto *IFD = dyn_cast<IndirectFieldDecl>(D))
6350+
FD = IFD->getAnonField();
6351+
else if (auto *DFD = dyn_cast<FieldDecl>(D))
6352+
FD = DFD;
6353+
else
6354+
continue;
6355+
63486356
// FIXME: Make use of previous designators to mark any fields before those
63496357
// inaccessible, and also compute the next initializer priority.
63506358
ResultBuilder::Result Result(FD, Results.getBasePriority(FD));

clang/test/CodeCompletion/desig-init.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@ namespace signature_regression {
7777
// CHECK-SIGNATURE-REGRESSION-NOT: OVERLOAD: [#int#]wrongFunction
7878
}
7979

80+
struct WithAnon {
81+
int outer;
82+
struct { int inner; };
83+
};
84+
auto TestWithAnon = WithAnon { .inner = 2 };
85+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:84:33 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC5 %s
86+
// CHECK-CC5: COMPLETION: inner : [#int#]inner
87+
// CHECK-CC5: COMPLETION: outer : [#int#]outer

0 commit comments

Comments
 (0)