diff options
| author | Erik Verbruggen <erikjv@me.com> | 2017-07-19 10:45:40 +0000 |
|---|---|---|
| committer | Erik Verbruggen <erikjv@me.com> | 2017-07-19 10:45:40 +0000 |
| commit | 11338c598a204f7dc1b3d60df26de4f93cf43223 (patch) | |
| tree | 88472ee1358bb6c2058bb4223aaae7e4e7bce3c1 /clang/lib/Sema/SemaCodeComplete.cpp | |
| parent | 76ea96bbce0e777874d2d909016049a6c14ad63e (diff) | |
| download | bcm5719-llvm-11338c598a204f7dc1b3d60df26de4f93cf43223.tar.gz bcm5719-llvm-11338c598a204f7dc1b3d60df26de4f93cf43223.zip | |
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
llvm-svn: 308433
Diffstat (limited to 'clang/lib/Sema/SemaCodeComplete.cpp')
| -rw-r--r-- | clang/lib/Sema/SemaCodeComplete.cpp | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index 3a53f251b09..f4f42e87da3 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -2398,6 +2398,37 @@ formatBlockPlaceholder(const PrintingPolicy &Policy, const NamedDecl *BlockDecl, 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(Preprocessor &PP, // 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(ASTContext &Context, // 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( |

