Skip to content

Commit 3a2f0e4

Browse files
committed
[CodeComplete] Complete a lambda when preferred type is a function
Summary: Uses a heuristic to detect std::function and friends. Reviewers: kadircet Reviewed By: kadircet Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62238 llvm-svn: 361461
1 parent 4a7da98 commit 3a2f0e4

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,6 +4108,69 @@ static void AddEnumerators(ResultBuilder &Results, ASTContext &Context,
41084108
Results.ExitScope();
41094109
}
41104110

4111+
/// Try to find a corresponding FunctionProtoType for function-like types (e.g.
4112+
/// function pointers, std::function, etc).
4113+
static const FunctionProtoType *TryDeconstructFunctionLike(QualType T) {
4114+
assert(!T.isNull());
4115+
// Try to extract first template argument from std::function<> and similar.
4116+
// Note we only handle the sugared types, they closely match what users wrote.
4117+
// We explicitly choose to not handle ClassTemplateSpecializationDecl.
4118+
if (auto *Specialization = T->getAs<TemplateSpecializationType>()) {
4119+
if (Specialization->getNumArgs() != 1)
4120+
return nullptr;
4121+
const TemplateArgument &Argument = Specialization->getArg(0);
4122+
if (Argument.getKind() != TemplateArgument::Type)
4123+
return nullptr;
4124+
return Argument.getAsType()->getAs<FunctionProtoType>();
4125+
}
4126+
// Handle other cases.
4127+
if (T->isPointerType())
4128+
T = T->getPointeeType();
4129+
return T->getAs<FunctionProtoType>();
4130+
}
4131+
4132+
/// Adds a pattern completion for a lambda expression with the specified
4133+
/// parameter types and placeholders for parameter names.
4134+
static void AddLambdaCompletion(ResultBuilder &Results,
4135+
llvm::ArrayRef<QualType> Parameters,
4136+
const LangOptions &LangOpts) {
4137+
CodeCompletionBuilder Completion(Results.getAllocator(),
4138+
Results.getCodeCompletionTUInfo());
4139+
// [](<parameters>) {}
4140+
Completion.AddChunk(CodeCompletionString::CK_LeftBracket);
4141+
Completion.AddPlaceholderChunk("=");
4142+
Completion.AddChunk(CodeCompletionString::CK_RightBracket);
4143+
if (!Parameters.empty()) {
4144+
Completion.AddChunk(CodeCompletionString::CK_LeftParen);
4145+
bool First = true;
4146+
for (auto Parameter : Parameters) {
4147+
if (!First)
4148+
Completion.AddChunk(CodeCompletionString::ChunkKind::CK_Comma);
4149+
else
4150+
First = false;
4151+
4152+
constexpr llvm::StringLiteral NamePlaceholder = "!#!NAME_GOES_HERE!#!";
4153+
std::string Type = NamePlaceholder;
4154+
Parameter.getAsStringInternal(Type, PrintingPolicy(LangOpts));
4155+
llvm::StringRef Prefix, Suffix;
4156+
std::tie(Prefix, Suffix) = llvm::StringRef(Type).split(NamePlaceholder);
4157+
Prefix = Prefix.rtrim();
4158+
Suffix = Suffix.ltrim();
4159+
4160+
Completion.AddTextChunk(Completion.getAllocator().CopyString(Prefix));
4161+
Completion.AddChunk(CodeCompletionString::CK_HorizontalSpace);
4162+
Completion.AddPlaceholderChunk("parameter");
4163+
Completion.AddTextChunk(Completion.getAllocator().CopyString(Suffix));
4164+
};
4165+
Completion.AddChunk(CodeCompletionString::CK_RightParen);
4166+
}
4167+
Completion.AddChunk(CodeCompletionString::CK_LeftBrace);
4168+
Completion.AddPlaceholderChunk("body");
4169+
Completion.AddChunk(CodeCompletionString::CK_RightBrace);
4170+
4171+
Results.AddResult(Completion.TakeString());
4172+
}
4173+
41114174
/// Perform code-completion in an expression context when we know what
41124175
/// type we're looking for.
41134176
void Sema::CodeCompleteExpression(Scope *S,
@@ -4169,6 +4232,14 @@ void Sema::CodeCompleteExpression(Scope *S,
41694232
if (CodeCompleter->includeMacros())
41704233
AddMacroResults(PP, Results, CodeCompleter->loadExternal(), false,
41714234
PreferredTypeIsPointer);
4235+
4236+
// Complete a lambda expression when preferred type is a function.
4237+
if (!Data.PreferredType.isNull() && getLangOpts().CPlusPlus11) {
4238+
if (const FunctionProtoType *F =
4239+
TryDeconstructFunctionLike(Data.PreferredType))
4240+
AddLambdaCompletion(Results, F->getParamTypes(), getLangOpts());
4241+
}
4242+
41724243
HandleCodeCompleteResults(this, CodeCompleter, Results.getCompletionContext(),
41734244
Results.data(), Results.size());
41744245
}

clang/test/CodeCompletion/lambdas.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
template <class T>
2+
struct function {
3+
};
4+
5+
6+
void test() {
7+
void (*x)(int, double) = nullptr;
8+
9+
function<void(int, double)> y = {};
10+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:7:28 %s -o - | FileCheck -check-prefix=CHECK-1 %s
11+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:9:35 %s -o - | FileCheck -check-prefix=CHECK-1 %s
12+
// CHECK-1: COMPLETION: Pattern : [<#=#>](int <#parameter#>, double <#parameter#>){<#body#>}
13+
14+
// == Placeholders for suffix types must be placed properly.
15+
function<void(void(*)(int))> z = {};
16+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:15:36 %s -o - | FileCheck -check-prefix=CHECK-2 %s
17+
// CHECK-2: COMPLETION: Pattern : [<#=#>](void (* <#parameter#>)(int)){<#body#>}
18+
19+
// == No need for a parameter list if function has no parameters.
20+
function<void()> a = {};
21+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:20:24 %s -o - | FileCheck -check-prefix=CHECK-3 %s
22+
// CHECK-3: COMPLETION: Pattern : [<#=#>]{<#body#>}
23+
}
24+
25+
template <class T, class Allocator = int>
26+
struct vector {};
27+
28+
void test2() {
29+
// == Try to preserve types as written.
30+
function<void(vector<int>)> a = {};
31+
32+
using function_typedef = function<void(vector<int>)>;
33+
function_typedef b = {};
34+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:30:35 %s -o - | FileCheck -check-prefix=CHECK-4 %s
35+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:33:24 %s -o - | FileCheck -check-prefix=CHECK-4 %s
36+
// CHECK-4: COMPLETION: Pattern : [<#=#>](vector<int> <#parameter#>){<#body#>}
37+
}
38+
39+
// Check another common function wrapper name.
40+
template <class T> struct unique_function {};
41+
42+
void test3() {
43+
unique_function<void()> a = {};
44+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:43:31 %s -o - | FileCheck -check-prefix=CHECK-5 %s
45+
// CHECK-5: COMPLETION: Pattern : [<#=#>]{<#body#>}
46+
}
47+
48+
template <class T, class U> struct weird_function {};
49+
void test4() {
50+
weird_function<void(), int> b = {};
51+
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:50:35 %s -o - | FileCheck -check-prefix=CHECK-6 %s
52+
// CHECK-6-NOT: COMPLETION: Pattern : [<#=
53+
}

0 commit comments

Comments
 (0)