r308433 - Add default values for function parameter chunks
Erik Verbruggen via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 19 03:45:40 PDT 2017
Author: erikjv
Date: Wed Jul 19 03:45:40 2017
New Revision: 308433
URL: http://llvm.org/viewvc/llvm-project?rev=308433&view=rev
Log:
Add default values for function parameter chunks
Append optional chunks with their default values. For example:
before - "int i", after - "int i = 10"
Patch by Ivan Donchevskii!
Differential Revision: https://reviews.llvm.org/D33644
Modified:
cfe/trunk/lib/Sema/SemaCodeComplete.cpp
cfe/trunk/test/CodeCompletion/functions.cpp
cfe/trunk/test/Index/code-completion.cpp
cfe/trunk/test/Index/complete-optional-params.cpp
Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=308433&r1=308432&r2=308433&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Wed Jul 19 03:45:40 2017
@@ -2398,6 +2398,37 @@ formatBlockPlaceholder(const PrintingPol
return Result;
}
+static std::string GetDefaultValueString(const ParmVarDecl *Param,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ const Expr *defaultArg = Param->getDefaultArg();
+ if (!defaultArg)
+ return "";
+ const SourceRange SrcRange = defaultArg->getSourceRange();
+ CharSourceRange CharSrcRange = CharSourceRange::getTokenRange(SrcRange);
+ bool Invalid = CharSrcRange.isInvalid();
+ if (Invalid)
+ return "";
+ StringRef srcText = Lexer::getSourceText(CharSrcRange, SM, LangOpts, &Invalid);
+ if (Invalid)
+ return "";
+
+ if (srcText.empty() || srcText == "=") {
+ // Lexer can't determine the value.
+ // This happens if the code is incorrect (for example class is forward declared).
+ return "";
+ }
+ std::string DefValue{srcText};
+ // FIXME: remove this check if the Lexer::getSourceText value is fixed and
+ // this value always has (or always does not have) '=' in front of it
+ if (DefValue.at(0) != '=') {
+ // If we don't have '=' in front of value.
+ // Lexer returns built-in types values without '=' and user-defined types values with it.
+ return " = " + DefValue;
+ }
+ return " " + DefValue;
+}
+
/// \brief Add function parameter chunks to the given code completion string.
static void AddFunctionParameterChunks(Preprocessor &PP,
const PrintingPolicy &Policy,
@@ -2431,6 +2462,8 @@ static void AddFunctionParameterChunks(P
// Format the placeholder string.
std::string PlaceholderStr = FormatFunctionParameter(Policy, Param);
+ if (Param->hasDefaultArg())
+ PlaceholderStr += GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts());
if (Function->isVariadic() && P == N - 1)
PlaceholderStr += ", ...";
@@ -3012,10 +3045,14 @@ static void AddOverloadParameterChunks(A
// Format the placeholder string.
std::string Placeholder;
- if (Function)
- Placeholder = FormatFunctionParameter(Policy, Function->getParamDecl(P));
- else
+ if (Function) {
+ const ParmVarDecl *Param = Function->getParamDecl(P);
+ Placeholder = FormatFunctionParameter(Policy, Param);
+ if (Param->hasDefaultArg())
+ Placeholder += GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts());
+ } else {
Placeholder = Prototype->getParamType(P).getAsString(Policy);
+ }
if (P == CurrentArg)
Result.AddCurrentParameterChunk(
Modified: cfe/trunk/test/CodeCompletion/functions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeCompletion/functions.cpp?rev=308433&r1=308432&r2=308433&view=diff
==============================================================================
--- cfe/trunk/test/CodeCompletion/functions.cpp (original)
+++ cfe/trunk/test/CodeCompletion/functions.cpp Wed Jul 19 03:45:40 2017
@@ -4,5 +4,5 @@ void f(float x, float y...);
void test() {
::
// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:5:5 %s -o - | FileCheck -check-prefix=CHECK-CC1 %s
- // CHECK-CC1: f(<#int i#>{#, <#int j#>{#, <#int k#>#}#})
+ // CHECK-CC1: f(<#int i#>{#, <#int j = 2#>{#, <#int k = 5#>#}#})
// CHECK-CC1: f(<#float x#>, <#float y, ...#>)
Modified: cfe/trunk/test/Index/code-completion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/code-completion.cpp?rev=308433&r1=308432&r2=308433&view=diff
==============================================================================
--- cfe/trunk/test/Index/code-completion.cpp (original)
+++ cfe/trunk/test/Index/code-completion.cpp Wed Jul 19 03:45:40 2017
@@ -40,7 +40,7 @@ Z::operator int() const {
// CHECK-MEMBER: FieldDecl:{ResultType double}{TypedText member}
// CHECK-MEMBER: FieldDecl:{ResultType int}{Text X::}{TypedText member}
// CHECK-MEMBER: FieldDecl:{ResultType float}{Text Y::}{TypedText member}
-// CHECK-MEMBER: CXXMethod:{ResultType void}{Informative Y::}{TypedText memfunc}{LeftParen (}{Optional {Placeholder int i}}{RightParen )}
+// CHECK-MEMBER: CXXMethod:{ResultType void}{Informative Y::}{TypedText memfunc}{LeftParen (}{Optional {Placeholder int i = 17}}{RightParen )}
// CHECK-MEMBER: CXXConversion:{TypedText operator int}{LeftParen (}{RightParen )}{Informative const}
// CHECK-MEMBER: CXXMethod:{ResultType Z &}{TypedText operator=}{LeftParen (}{Placeholder const Z &}{RightParen )}
// CHECK-MEMBER: CXXMethod:{ResultType X &}{Text X::}{TypedText operator=}{LeftParen (}{Placeholder const X &}{RightParen )}
@@ -77,7 +77,7 @@ Z::operator int() const {
// CHECK-EXPR: FieldDecl:{ResultType double}{TypedText member} (17)
// CHECK-EXPR: FieldDecl:{ResultType int}{Text X::}{TypedText member} (9)
// CHECK-EXPR: FieldDecl:{ResultType float}{Text Y::}{TypedText member} (18)
-// CHECK-EXPR: CXXMethod:{ResultType void}{TypedText memfunc}{LeftParen (}{Optional {Placeholder int i}}{RightParen )} (37)
+// CHECK-EXPR: CXXMethod:{ResultType void}{TypedText memfunc}{LeftParen (}{Optional {Placeholder int i = 17}}{RightParen )} (37)
// CHECK-EXPR: Namespace:{TypedText N}{Text ::} (75)
// CHECK-EXPR: Completion contexts:
// CHECK-EXPR-NEXT: Any type
Modified: cfe/trunk/test/Index/complete-optional-params.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/complete-optional-params.cpp?rev=308433&r1=308432&r2=308433&view=diff
==============================================================================
--- cfe/trunk/test/Index/complete-optional-params.cpp (original)
+++ cfe/trunk/test/Index/complete-optional-params.cpp Wed Jul 19 03:45:40 2017
@@ -6,15 +6,19 @@ void bar(int a, int b = 42, int c = 42);
void baz(int a = 42, ...);
struct S{ S(int a = 42, int = 42) {} };
+class Bar1 { public: Bar1() {} }; class Bar2;
+void foo_2(Bar1 b1 = Bar1(), Bar2 b2 = Bar2());
+
int main() {
foo(42, 42);
bar(42, 42, 42);
baz(42, 42, 42);
S s(42, 42);
+ foo_2();
}
-// RUN: c-index-test -code-completion-at=%s:10:9 %s | FileCheck -check-prefix=CHECK-CC1 %s
-// CHECK-CC1: OverloadCandidate:{ResultType void}{Text foo}{LeftParen (}{Optional {CurrentParameter int a}{Optional {Comma , }{Placeholder int}}}{RightParen )} (1)
+// RUN: c-index-test -code-completion-at=%s:13:9 %s | FileCheck -check-prefix=CHECK-CC1 %s
+// CHECK-CC1: OverloadCandidate:{ResultType void}{Text foo}{LeftParen (}{Optional {CurrentParameter int a = 42}{Optional {Comma , }{Placeholder int = 42}}}{RightParen )} (1)
// CHECK-CC1: Completion contexts:
// CHECK-CC1-NEXT: Any type
// CHECK-CC1-NEXT: Any value
@@ -25,8 +29,8 @@ int main() {
// CHECK-CC1-NEXT: Nested name specifier
// CHECK-CC1-NEXT: Objective-C interface
-// RUN: c-index-test -code-completion-at=%s:11:9 %s | FileCheck -check-prefix=CHECK-CC2 %s
-// CHECK-CC2: OverloadCandidate:{ResultType void}{Text bar}{LeftParen (}{CurrentParameter int a}{Optional {Comma , }{Placeholder int b}{Optional {Comma , }{Placeholder int c}}}{RightParen )} (1)
+// RUN: c-index-test -code-completion-at=%s:14:9 %s | FileCheck -check-prefix=CHECK-CC2 %s
+// CHECK-CC2: OverloadCandidate:{ResultType void}{Text bar}{LeftParen (}{CurrentParameter int a}{Optional {Comma , }{Placeholder int b = 42}{Optional {Comma , }{Placeholder int c = 42}}}{RightParen )} (1)
// CHECK-CC2: Completion contexts:
// CHECK-CC2-NEXT: Any type
// CHECK-CC2-NEXT: Any value
@@ -37,8 +41,8 @@ int main() {
// CHECK-CC2-NEXT: Nested name specifier
// CHECK-CC2-NEXT: Objective-C interface
-// RUN: c-index-test -code-completion-at=%s:11:16 %s | FileCheck -check-prefix=CHECK-CC3 %s
-// CHECK-CC3: OverloadCandidate:{ResultType void}{Text bar}{LeftParen (}{Placeholder int a}{Optional {Comma , }{Placeholder int b}{Optional {Comma , }{CurrentParameter int c}}}{RightParen )} (1)
+// RUN: c-index-test -code-completion-at=%s:14:16 %s | FileCheck -check-prefix=CHECK-CC3 %s
+// CHECK-CC3: OverloadCandidate:{ResultType void}{Text bar}{LeftParen (}{Placeholder int a}{Optional {Comma , }{Placeholder int b = 42}{Optional {Comma , }{CurrentParameter int c = 42}}}{RightParen )} (1)
// CHECK-CC3: Completion contexts:
// CHECK-CC3-NEXT: Any type
// CHECK-CC3-NEXT: Any value
@@ -49,8 +53,8 @@ int main() {
// CHECK-CC3-NEXT: Nested name specifier
// CHECK-CC3-NEXT: Objective-C interface
-// RUN: c-index-test -code-completion-at=%s:12:16 %s | FileCheck -check-prefix=CHECK-CC4 %s
-// CHECK-CC4: OverloadCandidate:{ResultType void}{Text baz}{LeftParen (}{Optional {Placeholder int a}{Optional {Comma , }{CurrentParameter ...}}}{RightParen )} (1)
+// RUN: c-index-test -code-completion-at=%s:15:16 %s | FileCheck -check-prefix=CHECK-CC4 %s
+// CHECK-CC4: OverloadCandidate:{ResultType void}{Text baz}{LeftParen (}{Optional {Placeholder int a = 42}{Optional {Comma , }{CurrentParameter ...}}}{RightParen )} (1)
// CHECK-CC4: Completion contexts:
// CHECK-CC4-NEXT: Any type
// CHECK-CC4-NEXT: Any value
@@ -61,8 +65,8 @@ int main() {
// CHECK-CC4-NEXT: Nested name specifier
// CHECK-CC4-NEXT: Objective-C interface
-// RUN: c-index-test -code-completion-at=%s:13:9 %s | FileCheck -check-prefix=CHECK-CC5 %s
-// CHECK-CC5: OverloadCandidate:{Text S}{LeftParen (}{Optional {CurrentParameter int a}{Optional {Comma , }{Placeholder int}}}{RightParen )} (1)
+// RUN: c-index-test -code-completion-at=%s:16:9 %s | FileCheck -check-prefix=CHECK-CC5 %s
+// CHECK-CC5: OverloadCandidate:{Text S}{LeftParen (}{Optional {CurrentParameter int a = 42}{Optional {Comma , }{Placeholder int = 42}}}{RightParen )} (1)
// CHECK-CC5: OverloadCandidate:{Text S}{LeftParen (}{CurrentParameter const S &}{RightParen )} (1)
// CHECK-CC5: Completion contexts:
// CHECK-CC5-NEXT: Any type
@@ -73,3 +77,15 @@ int main() {
// CHECK-CC5-NEXT: Class name
// CHECK-CC5-NEXT: Nested name specifier
// CHECK-CC5-NEXT: Objective-C interface
+
+// RUN: c-index-test -code-completion-at=%s:17:11 %s | FileCheck -check-prefix=CHECK-CC6 %s
+// CHECK-CC6: FunctionDecl:{ResultType void}{TypedText foo_2}{LeftParen (}{Optional {Placeholder Bar1 b1 = Bar1()}{Optional {Comma , }{Placeholder Bar2 b2}}}{RightParen )} (50)
+// CHECK-CC6: Completion contexts:
+// CHECK-CC6-NEXT: Any type
+// CHECK-CC6-NEXT: Any value
+// CHECK-CC6-NEXT: Enum tag
+// CHECK-CC6-NEXT: Union tag
+// CHECK-CC6-NEXT: Struct tag
+// CHECK-CC6-NEXT: Class name
+// CHECK-CC6-NEXT: Nested name specifier
+// CHECK-CC6-NEXT: Objective-C interface
More information about the cfe-commits
mailing list