ilya-biryukov created this revision. ilya-biryukov added a reviewer: kadircet. Herald added a project: clang.
Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D72497 Files: clang/lib/Sema/SemaCodeComplete.cpp clang/test/CodeCompletion/patterns.cpp Index: clang/test/CodeCompletion/patterns.cpp =================================================================== --- clang/test/CodeCompletion/patterns.cpp +++ clang/test/CodeCompletion/patterns.cpp @@ -33,20 +33,44 @@ bool bool_return() { // line 34 } +int *ptr_return() { + // line 37 +} +struct Cls {}; +int Cls::*memptr_return() { + // line 41 +} // 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 false; // RETURN-VAL-NOT: COMPLETION: Pattern : return true; +// RETURN-VAL-NOT: COMPLETION: Pattern : return nullptr; // 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 false; // RETURN-VOID-NOT: COMPLETION: Pattern : return true; // RETURN-VOID-NOT: COMPLETION: Pattern : return <#expression#>; +// RETURN-VOID-NOT: COMPLETION: Pattern : return nullptr; // 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-NOT: COMPLETION: Pattern : return nullptr; // RETURN-BOOL: COMPLETION: Pattern : return <#expression#>;{{$}} // RETURN-BOOL: COMPLETION: Pattern : return false;{{$}} // RETURN-BOOL: COMPLETION: Pattern : return true;{{$}} + +// Check both pointer and member pointer return types. +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -code-completion-patterns -code-completion-at=%s:37:1 %s -o - | FileCheck -check-prefix=RETURN-PTR %s +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -code-completion-patterns -code-completion-at=%s:41:1 %s -o - | FileCheck -check-prefix=RETURN-PTR %s +// RETURN-PTR-NOT: COMPLETION: Pattern : return false;{{$}} +// RETURN-PTR-NOT: COMPLETION: Pattern : return true;{{$}} +// RETURN-PTR-NOT: COMPLETION: Pattern : return; +// RETURN-PTR: COMPLETION: Pattern : return <#expression#>;{{$}} +// RETURN-PTR: COMPLETION: Pattern : return nullptr; + +// 'return nullptr' is not available before C++11. +// RUN: %clang_cc1 -fsyntax-only -std=c++03 -code-completion-patterns -code-completion-at=%s:37:1 %s -o - | FileCheck -check-prefix=RETURN-PTR-STD03 %s +// RUN: %clang_cc1 -fsyntax-only -std=c++03 -code-completion-patterns -code-completion-at=%s:41:1 %s -o - | FileCheck -check-prefix=RETURN-PTR-STD03 %s +// RETURN-PTR-STD03-NOT: COMPLETION: Pattern : return nullptr; Index: clang/lib/Sema/SemaCodeComplete.cpp =================================================================== --- clang/lib/Sema/SemaCodeComplete.cpp +++ clang/lib/Sema/SemaCodeComplete.cpp @@ -2312,6 +2312,13 @@ Builder.AddChunk(CodeCompletionString::CK_SemiColon); Results.AddResult(Result(Builder.TakeString())); } + // For pointers, suggest 'return nullptr' in C++. + if (SemaRef.getLangOpts().CPlusPlus11 && + (ReturnType->isPointerType() || ReturnType->isMemberPointerType())) { + Builder.AddTypedTextChunk("return nullptr"); + Builder.AddChunk(CodeCompletionString::CK_SemiColon); + Results.AddResult(Result(Builder.TakeString())); + } } // goto identifier ;
Index: clang/test/CodeCompletion/patterns.cpp =================================================================== --- clang/test/CodeCompletion/patterns.cpp +++ clang/test/CodeCompletion/patterns.cpp @@ -33,20 +33,44 @@ bool bool_return() { // line 34 } +int *ptr_return() { + // line 37 +} +struct Cls {}; +int Cls::*memptr_return() { + // line 41 +} // 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 false; // RETURN-VAL-NOT: COMPLETION: Pattern : return true; +// RETURN-VAL-NOT: COMPLETION: Pattern : return nullptr; // 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 false; // RETURN-VOID-NOT: COMPLETION: Pattern : return true; // RETURN-VOID-NOT: COMPLETION: Pattern : return <#expression#>; +// RETURN-VOID-NOT: COMPLETION: Pattern : return nullptr; // 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-NOT: COMPLETION: Pattern : return nullptr; // RETURN-BOOL: COMPLETION: Pattern : return <#expression#>;{{$}} // RETURN-BOOL: COMPLETION: Pattern : return false;{{$}} // RETURN-BOOL: COMPLETION: Pattern : return true;{{$}} + +// Check both pointer and member pointer return types. +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -code-completion-patterns -code-completion-at=%s:37:1 %s -o - | FileCheck -check-prefix=RETURN-PTR %s +// RUN: %clang_cc1 -fsyntax-only -std=c++11 -code-completion-patterns -code-completion-at=%s:41:1 %s -o - | FileCheck -check-prefix=RETURN-PTR %s +// RETURN-PTR-NOT: COMPLETION: Pattern : return false;{{$}} +// RETURN-PTR-NOT: COMPLETION: Pattern : return true;{{$}} +// RETURN-PTR-NOT: COMPLETION: Pattern : return; +// RETURN-PTR: COMPLETION: Pattern : return <#expression#>;{{$}} +// RETURN-PTR: COMPLETION: Pattern : return nullptr; + +// 'return nullptr' is not available before C++11. +// RUN: %clang_cc1 -fsyntax-only -std=c++03 -code-completion-patterns -code-completion-at=%s:37:1 %s -o - | FileCheck -check-prefix=RETURN-PTR-STD03 %s +// RUN: %clang_cc1 -fsyntax-only -std=c++03 -code-completion-patterns -code-completion-at=%s:41:1 %s -o - | FileCheck -check-prefix=RETURN-PTR-STD03 %s +// RETURN-PTR-STD03-NOT: COMPLETION: Pattern : return nullptr; Index: clang/lib/Sema/SemaCodeComplete.cpp =================================================================== --- clang/lib/Sema/SemaCodeComplete.cpp +++ clang/lib/Sema/SemaCodeComplete.cpp @@ -2312,6 +2312,13 @@ Builder.AddChunk(CodeCompletionString::CK_SemiColon); Results.AddResult(Result(Builder.TakeString())); } + // For pointers, suggest 'return nullptr' in C++. + if (SemaRef.getLangOpts().CPlusPlus11 && + (ReturnType->isPointerType() || ReturnType->isMemberPointerType())) { + Builder.AddTypedTextChunk("return nullptr"); + Builder.AddChunk(CodeCompletionString::CK_SemiColon); + Results.AddResult(Result(Builder.TakeString())); + } } // goto identifier ;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits