[PATCH] D33644: Add default values for function parameter chunks

Ivan Donchevskii via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 19 04:51:05 PDT 2017


yvvan updated this revision to Diff 103016.
yvvan added a comment.

Do not evaluate numbers.
Check for != "=" is needed not to mess with invalid default arguments or their types (without it I get "const Bar& bar = =" when Bar is not defined)


https://reviews.llvm.org/D33644

Files:
  lib/Sema/SemaCodeComplete.cpp


Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -2351,6 +2351,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,
@@ -2379,11 +2386,17 @@
       FirstParameter = false;
     else
       Result.AddChunk(CodeCompletionString::CK_Comma);
-    
+
     InOptional = false;
     
     // Format the placeholder string.
     std::string PlaceholderStr = FormatFunctionParameter(Policy, Param);
+    if (Param->hasDefaultArg()) {
+        std::string DefaultValue =
+                GetDefaultValueString(Param, PP.getSourceManager(), PP.getLangOpts());
+        if (!DefaultValue.empty() && DefaultValue != "=")
+            PlaceholderStr += " = " + DefaultValue;
+    }
 
     if (Function->isVariadic() && P == N - 1)
       PlaceholderStr += ", ...";
@@ -2964,10 +2977,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()) {
+        std::string DefaultValue =
+                GetDefaultValueString(Param, Context.getSourceManager(), Context.getLangOpts());
+        if (!DefaultValue.empty() && DefaultValue != "=")
+          Placeholder += " = " + DefaultValue;
+      }
+    } else {
       Placeholder = Prototype->getParamType(P).getAsString(Policy);
+    }
 
     if (P == CurrentArg)
       Result.AddCurrentParameterChunk(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33644.103016.patch
Type: text/x-patch
Size: 2228 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170619/fdb5260a/attachment.bin>


More information about the cfe-commits mailing list