r361753 - [CodeComplete] Complete 'return true/false' in boolean functions
Ilya Biryukov via cfe-commits
cfe-commits at lists.llvm.org
Mon May 27 02:52:10 PDT 2019
Author: ibiryukov
Date: Mon May 27 02:52:09 2019
New Revision: 361753
URL: http://llvm.org/viewvc/llvm-project?rev=361753&view=rev
Log:
[CodeComplete] Complete 'return true/false' in boolean functions
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D62391
Modified:
cfe/trunk/lib/Sema/SemaCodeComplete.cpp
cfe/trunk/test/CodeCompletion/patterns.cpp
Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=361753&r1=361752&r2=361753&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Mon May 27 02:52:09 2019
@@ -2168,23 +2168,38 @@ static void AddOrdinaryNameResults(Sema:
Results.AddResult(Result(Builder.TakeString()));
}
- // "return expression ;" or "return ;", depending on whether we
- // know the function is void or not.
- bool isVoid = false;
+ // "return expression ;" or "return ;", depending on the return type.
+ QualType ReturnType;
if (const auto *Function = dyn_cast<FunctionDecl>(SemaRef.CurContext))
- isVoid = Function->getReturnType()->isVoidType();
+ ReturnType = Function->getReturnType();
else if (const auto *Method = dyn_cast<ObjCMethodDecl>(SemaRef.CurContext))
- isVoid = Method->getReturnType()->isVoidType();
+ ReturnType = Method->getReturnType();
else if (SemaRef.getCurBlock() &&
!SemaRef.getCurBlock()->ReturnType.isNull())
- isVoid = SemaRef.getCurBlock()->ReturnType->isVoidType();
- Builder.AddTypedTextChunk("return");
- if (!isVoid) {
- Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
+ ReturnType = SemaRef.getCurBlock()->ReturnType;;
+ if (ReturnType.isNull() || ReturnType->isVoidType()) {
+ Builder.AddTypedTextChunk("return");
+ Builder.AddChunk(CodeCompletionString::CK_SemiColon);
+ Results.AddResult(Result(Builder.TakeString()));
+ } else {
+ assert(!ReturnType.isNull());
+ // "return expression ;"
+ Builder.AddTypedTextChunk("return");
+ Builder.AddChunk(clang::CodeCompletionString::CK_HorizontalSpace);
Builder.AddPlaceholderChunk("expression");
+ Builder.AddChunk(CodeCompletionString::CK_SemiColon);
+ Results.AddResult(Result(Builder.TakeString()));
+ // When boolean, also add 'return true;' and 'return false;'.
+ if (ReturnType->isBooleanType()) {
+ Builder.AddTypedTextChunk("return true");
+ Builder.AddChunk(CodeCompletionString::CK_SemiColon);
+ Results.AddResult(Result(Builder.TakeString()));
+
+ Builder.AddTypedTextChunk("return false");
+ Builder.AddChunk(CodeCompletionString::CK_SemiColon);
+ Results.AddResult(Result(Builder.TakeString()));
+ }
}
- Builder.AddChunk(CodeCompletionString::CK_SemiColon);
- Results.AddResult(Result(Builder.TakeString()));
// goto identifier ;
Builder.AddTypedTextChunk("goto");
Modified: cfe/trunk/test/CodeCompletion/patterns.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeCompletion/patterns.cpp?rev=361753&r1=361752&r2=361753&view=diff
==============================================================================
--- cfe/trunk/test/CodeCompletion/patterns.cpp (original)
+++ cfe/trunk/test/CodeCompletion/patterns.cpp Mon May 27 02:52:09 2019
@@ -30,10 +30,23 @@ int value_return() {
void void_return() {
// line 31
}
+bool bool_return() {
+ // line 34
+}
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:28:1 %s -o - | FileCheck -check-prefix=RETURN-VAL %s
-// RETURN-VAL-NOT: COMPLETION: Pattern : return;{{$}}
+// RETURN-VAL-NOT: COMPLETION: Pattern : return;
+// RETURN-VAL-NOT: COMPLETION: Pattern : return false;
+// RETURN-VAL-NOT: COMPLETION: Pattern : return true;
// RETURN-VAL: COMPLETION: Pattern : return <#expression#>;{{$}}
// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:31:1 %s -o - | FileCheck -check-prefix=RETURN-VOID %s
-// RETURN-VOID-NOT: COMPLETION: Pattern : return <#expression#>;{{$}}
+// RETURN-VOID-NOT: COMPLETION: Pattern : return false;
+// RETURN-VOID-NOT: COMPLETION: Pattern : return true;
+// RETURN-VOID-NOT: COMPLETION: Pattern : return <#expression#>;
// RETURN-VOID: COMPLETION: Pattern : return;{{$}}
+
+// RUN: %clang_cc1 -fsyntax-only -code-completion-patterns -code-completion-at=%s:34:1 %s -o - | FileCheck -check-prefix=RETURN-BOOL %s
+// RETURN-BOOL-NOT: COMPLETION: Pattern : return;
+// RETURN-BOOL: COMPLETION: Pattern : return <#expression#>;{{$}}
+// RETURN-BOOL: COMPLETION: Pattern : return false;{{$}}
+// RETURN-BOOL: COMPLETION: Pattern : return true;{{$}}
More information about the cfe-commits
mailing list