[PATCH] D33644: Add default values for function parameter chunks
Ivan Donchevskii via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 16 03:21:37 PDT 2017
yvvan updated this revision to Diff 102800.
yvvan added a comment.
Use Lexer::getSourceText
https://reviews.llvm.org/D33644
Files:
lib/Sema/SemaCodeComplete.cpp
Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -2282,6 +2282,15 @@
} else {
Type.getAsStringInternal(Result, Policy);
}
+ if (Param->hasDefaultArg()) {
+ APValue *defaultValue = Param->evaluateValue();
+ if (defaultValue) {
+ std::string defaultValueStr = defaultValue->getAsString(
+ Param->getASTContext(), Param->getType());
+ if (!defaultValueStr.empty())
+ Result += " = " + defaultValueStr;
+ }
+ }
return Result;
}
@@ -2398,6 +2407,13 @@
return Result;
}
+static std::string GetDefaultValueString(const ParmVarDecl *Param,
+ const SourceManager &SM,
+ const LangOptions &LangOpts) {
+ const SourceRange SrcRange = Param->getDefaultArg()->getSourceRange();
+ return Lexer::getSourceText(CharSourceRange::getTokenRange(SrcRange), SM, LangOpts);
+}
+
/// \brief Add function parameter chunks to the given code completion string.
static void AddFunctionParameterChunks(Preprocessor &PP,
const PrintingPolicy &Policy,
@@ -2426,11 +2442,17 @@
FirstParameter = false;
else
Result.AddChunk(CodeCompletionString::CK_Comma);
-
+
InOptional = false;
// Format the placeholder string.
std::string PlaceholderStr = FormatFunctionParameter(Policy, Param);
+ if (Param->hasDefaultArg() && PlaceholderStr.find("=") == std::string::npos) {
+ std::string DefaultValue =
+ GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts());
+ if (!DefaultValue.empty())
+ PlaceholderStr += " = " + DefaultValue;
+ }
if (Function->isVariadic() && P == N - 1)
PlaceholderStr += ", ...";
@@ -3012,10 +3034,18 @@
// 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.find("=") == std::string::npos) {
+ std::string DefaultValue =
+ GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts());
+ if (!DefaultValue.empty())
+ Placeholder += " = " + DefaultValue;
+ }
+ } else {
Placeholder = Prototype->getParamType(P).getAsString(Policy);
+ }
if (P == CurrentArg)
Result.AddCurrentParameterChunk(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33644.102800.patch
Type: text/x-patch
Size: 2745 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170616/848a2c43/attachment-0001.bin>
More information about the cfe-commits
mailing list