[clang] bbf234b - [CodeCompletion] Complete designators for fields in anonymous structs/unions

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 10 15:58:55 PST 2022


Author: Sam McCall
Date: 2022-01-11T00:58:49+01:00
New Revision: bbf234b56a82d1b9b7fa58f8b10f16d417500d9b

URL: https://github.com/llvm/llvm-project/commit/bbf234b56a82d1b9b7fa58f8b10f16d417500d9b
DIFF: https://github.com/llvm/llvm-project/commit/bbf234b56a82d1b9b7fa58f8b10f16d417500d9b.diff

LOG: [CodeCompletion] Complete designators for fields in anonymous structs/unions

Fixes https://github.com/clangd/clangd/issues/836

Differential Revision: https://reviews.llvm.org/D116717

Added: 
    

Modified: 
    clang/lib/Sema/SemaCodeComplete.cpp
    clang/test/CodeCompletion/desig-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index c075cbf536caf..9266deb6699af 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -6344,7 +6344,15 @@ void Sema::CodeCompleteDesignator(QualType BaseType,
                         CodeCompleter->getCodeCompletionTUInfo(), CCC);
 
   Results.EnterNewScope();
-  for (const auto *FD : RD->fields()) {
+  for (const Decl *D : RD->decls()) {
+    const FieldDecl *FD;
+    if (auto *IFD = dyn_cast<IndirectFieldDecl>(D))
+      FD = IFD->getAnonField();
+    else if (auto *DFD = dyn_cast<FieldDecl>(D))
+      FD = DFD;
+    else
+      continue;
+
     // FIXME: Make use of previous designators to mark any fields before those
     // inaccessible, and also compute the next initializer priority.
     ResultBuilder::Result Result(FD, Results.getBasePriority(FD));

diff  --git a/clang/test/CodeCompletion/desig-init.cpp b/clang/test/CodeCompletion/desig-init.cpp
index 999f368ba5634..76f6d2dea010d 100644
--- a/clang/test/CodeCompletion/desig-init.cpp
+++ b/clang/test/CodeCompletion/desig-init.cpp
@@ -77,3 +77,11 @@ namespace signature_regression {
   // CHECK-SIGNATURE-REGRESSION-NOT: OVERLOAD: [#int#]wrongFunction
 }
 
+struct WithAnon {
+  int outer;
+  struct { int inner; };
+};
+auto TestWithAnon = WithAnon { .inner = 2 };
+  // 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
+  // CHECK-CC5: COMPLETION: inner : [#int#]inner
+  // CHECK-CC5: COMPLETION: outer : [#int#]outer


        


More information about the cfe-commits mailing list