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(


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(
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to