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

Ivan Donchevskii via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 30 07:18:09 PDT 2017


yvvan updated this revision to Diff 100705.
yvvan edited the summary of this revision.
yvvan added a comment.

Support for non-simple types. Drop results with macros.


https://reviews.llvm.org/D33644

Files:
  lib/Sema/SemaCodeComplete.cpp


Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -2279,6 +2279,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;
   }
 
@@ -2395,6 +2404,19 @@
   return Result;
 }
 
+static std::string GetDefaultValueString(const ParmVarDecl *Param,
+                                         const SourceManager &SM,
+                                         const LangOptions &LangOpts) {
+  const SourceRange SrcRange = Param->getDefaultArg()->getSourceRange();
+  const SourceLocation StartLoc = SrcRange.getBegin();
+  const SourceLocation EndLoc = SrcRange.getEnd();
+  if (StartLoc != SM.getExpansionLoc(StartLoc) || EndLoc != SM.getExpansionLoc(EndLoc))
+      return "";
+  const size_t PtrDiff = EndLoc.getRawEncoding() - StartLoc.getRawEncoding()
+          + Lexer::MeasureTokenLength(EndLoc, SM, LangOpts);
+  return std::string{SM.getCharacterData(StartLoc), PtrDiff};
+}
+
 /// \brief Add function parameter chunks to the given code completion string.
 static void AddFunctionParameterChunks(Preprocessor &PP,
                                        const PrintingPolicy &Policy,
@@ -2423,11 +2445,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 += ", ...";
@@ -3009,10 +3037,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.100705.patch
Type: text/x-patch
Size: 3060 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170530/0485f58e/attachment.bin>


More information about the cfe-commits mailing list