Skip to content

Commit

Permalink
[clang] Enable code completion of designated initializers in Compound…
Browse files Browse the repository at this point in the history
… Literal Expressions

PreferedType were not set when parsing compound literals, hence
designated initializers were not available as code completion suggestions.

This patch sets the preferedtype to parsed type for the following initializer
list.

Fixes clangd/clangd#142.

Differential Revision: https://reviews.llvm.org/D92370
  • Loading branch information
kadircet committed Dec 1, 2020
1 parent efa9728 commit e98d3be
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
1 change: 1 addition & 0 deletions clang/lib/Parse/ParseExpr.cpp
Expand Up @@ -3111,6 +3111,7 @@ Parser::ParseCompoundLiteralExpression(ParsedType Ty,
assert(Tok.is(tok::l_brace) && "Not a compound literal!");
if (!getLangOpts().C99) // Compound literals don't exist in C90.
Diag(LParenLoc, diag::ext_c99_compound_literal);
PreferredType.enterTypeCast(Tok.getLocation(), Ty.get());
ExprResult Result = ParseInitializer();
if (!Result.isInvalid() && Ty)
return Actions.ActOnCompoundLiteral(LParenLoc, Ty, RParenLoc, Result.get());
Expand Down
8 changes: 5 additions & 3 deletions clang/test/CodeCompletion/desig-init.cpp
Expand Up @@ -23,9 +23,11 @@ void foo() {
auto z = [](Base B) {};
z({.t = 1});
z(Base{.t = 2});
z((Base){.t = 2});
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:22:14 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC2 %s
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:24:7 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC2 %s
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:25:11 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC2 %s
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:26:13 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC2 %s
// CHECK-CC2: COMPLETION: t : [#int#]t
}

Expand All @@ -39,16 +41,16 @@ struct Test<int> {
};
void bar() {
Test<char> T{.x = 2};
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:41:17 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC3 %s
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:43:17 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC3 %s
// CHECK-CC3: COMPLETION: x : [#T#]x
Test<int> X{.x = 2};
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:44:16 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC4 %s
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:46:16 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC4 %s
// CHECK-CC4: COMPLETION: x : [#int#]x
// CHECK-CC4-NEXT: COMPLETION: y : [#char#]y
}

template <typename T>
void aux() {
Test<T> X{.x = T(2)};
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:52:14 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC3 %s
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:54:14 %s -o - -std=c++2a | FileCheck -check-prefix=CHECK-CC3 %s
}

0 comments on commit e98d3be

Please sign in to comment.