[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdevelop/languages/cpp
From:       David Nolden <david.nolden.kde () art-master ! de>
Date:       2009-03-19 23:21:39
Message-ID: 1237504899.262645.31704.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 941564 by zwabel:

- Implement a new type shortening mechanism: Detect and remove template parameters \
                that match the default template parameters for the given type before \
                printing it.
- Use that shortening mechanism additionally in the completion-list and all the \
assistants/helpers. Now std::list<int> is shown and used, instead of std::list<int, \
std::allocator<int> >


 M  +1 -1      codecompletion/helpers.cpp  
 M  +4 -2      codecompletion/implementationhelperitem.cpp  
 M  +4 -1      codecompletion/item.cpp  
 M  +5 -4      codegen/signatureassistant.cpp  
 M  +88 -4     cppduchain/cppduchain.cpp  
 M  +6 -3      cppduchain/cppduchain.h  
 M  +2 -2      cppduchain/missingdeclarationassistant.cpp  
 M  +5 -5      cppduchain/sourcemanipulation.cpp  
 M  +3 -3      cppduchain/tests/test_duchain.cpp  


--- trunk/KDE/kdevelop/languages/cpp/codecompletion/helpers.cpp #941563:941564
@@ -144,7 +144,7 @@
         if(noShortening)
           ret += argument->toString();
         else
-          ret += Cpp::shortenedTypeString(*paramNameIt, desiredArgumentTypeLength, \
item.stripPrefix()); +          ret += Cpp::shortenedTypeString(*paramNameIt, top, \
desiredArgumentTypeLength, item.stripPrefix());  ret += " " + \
(*paramNameIt)->identifier().toString();  } else if (argument)
         ret += argument->toString();
--- trunk/KDE/kdevelop/languages/cpp/codecompletion/implementationhelperitem.cpp \
#941563:941564 @@ -28,6 +28,7 @@
 #include <interfaces/ilanguagecontroller.h>
 #include <language/duchain/parsingenvironment.h>
 #include <sourcemanipulation.h>
+#include <cppduchain.h>
 
 namespace Cpp {
 
@@ -111,13 +112,14 @@
   if(!m_declaration)
     return QString();
 
+  ///@todo Move these functionalities into sourcemanipulation.cpp
   if(m_type == Override) {
     if(!useAlternativeText) {
       if(m_declaration) {
         newText = "virtual ";
         FunctionType::Ptr asFunction = m_declaration->type<FunctionType>();
         if(asFunction && asFunction->returnType())
-            newText += asFunction->returnType()->toString() + " ";
+            newText += Cpp::simplifiedTypeString(asFunction->returnType(), \
completionContext()->duContext()) + " ";  
         newText += m_declaration->identifier().toString();
         newText += signaturePart(true);
@@ -153,7 +155,7 @@
       FunctionType::Ptr asFunction = m_declaration->type<FunctionType>();
       
       if(asFunction && asFunction->returnType())
-          newText += asFunction->returnType()->toString() + " ";
+          newText += Cpp::simplifiedTypeString(asFunction->returnType(), \
completionContext()->duContext()) + " ";  newText += scope.toString();
       newText += signaturePart(false);
       newText += " {\n";
--- trunk/KDE/kdevelop/languages/cpp/codecompletion/item.cpp #941563:941564
@@ -215,7 +215,10 @@
 
 QString NormalDeclarationCompletionItem::shortenedTypeString(KDevelop::DeclarationPointer \
decl, int desiredTypeLength) const  {
-  return Cpp::shortenedTypeString(decl.data(), desiredTypeLength);
+  if(completionContext() && completionContext()->duContext())
+    return Cpp::shortenedTypeString(decl.data(), (completionContext() && \
completionContext()->duContext()) ? completionContext()->duContext()->topContext() : \
0, desiredTypeLength); +  else
+    return QString("no type string available");
 }
 
 KDevelop::QualifiedIdentifier NormalDeclarationCompletionItem::stripPrefix() const {
--- trunk/KDE/kdevelop/languages/cpp/codegen/signatureassistant.cpp #941563:941564
@@ -35,6 +35,7 @@
 #include <language/duchain/types/functiontype.h>
 #include <language/duchain/parsingenvironment.h>
 #include "signatureassistant.h"
+#include "cppduchain.h"
 
 using namespace  KDevelop;
 using namespace Cpp;
@@ -145,12 +146,12 @@
   return 0;
 }
 
-QString makeSignatureString(QList<SignatureItem> signature) {
+QString makeSignatureString(QList<SignatureItem> signature, DUContext* \
visibilityFrom) {  QString ret;
   foreach(SignatureItem item, signature) {
     if(!ret.isEmpty())
       ret += ", ";
-    ret += (item.first.abstractType() ? item.first.abstractType()->toString() : \
QString("<none>")); +    ret += Cpp::simplifiedTypeString(item.first.abstractType(),  \
visibilityFrom);  
     if(!item.second.isEmpty())
       ret += " " + item.second;
@@ -172,7 +173,7 @@
     
     virtual QString description() const {
       KDevelop::DUChainReadLocker lock(KDevelop::DUChain::lock());
-      return i18n("Update Definition from %1(%2) to (%3)", \
m_otherSideId.qualifiedIdentifier().toString(), makeSignatureString(m_oldSignature), \
makeSignatureString(m_newSignature)); +      return i18n("Update Definition from \
%1(%2) to (%3)", m_otherSideId.qualifiedIdentifier().toString(), \
makeSignatureString(m_oldSignature, m_otherSideContext.data()), \
makeSignatureString(m_newSignature, m_otherSideContext.data()));  }
     
     virtual void execute() {
@@ -214,7 +215,7 @@
       }
       
       DocumentChangeSet changes;
-      DocumentChangePointer change(new DocumentChange(functionContext->url(), \
functionContext->range(), QString(), makeSignatureString(m_newSignature))); +      \
DocumentChangePointer change(new DocumentChange(functionContext->url(), \
functionContext->range(), QString(), makeSignatureString(m_newSignature, \
m_otherSideContext.data())));  change->m_ignoreOldText = true;
       changes.addChange( change );
       DocumentChangeSet::ChangeResult result = \
                changes.applyAllChanges(DocumentChangeSet::WarnOnFailedChange);
--- trunk/KDE/kdevelop/languages/cpp/cppduchain/cppduchain.cpp #941563:941564
@@ -499,8 +499,84 @@
   return type;
 }
 
+///Returns a type that has all template types replaced with DelayedType's that have \
their template default parameters stripped away. +///The returned type should not \
actively be used in the  type-system, but rather only for displaying. \
+AbstractType::Ptr stripTemplateDefaultParameters(KDevelop::AbstractType::Ptr type, \
TopDUContext* top) { +  
+  if(!type)
+    return AbstractType::Ptr();
+  
+  struct ShortenTemplateDefaultParameter : public KDevelop::TypeExchanger {
+    TopDUContext* top;
+    ShortenTemplateDefaultParameter(TopDUContext* _top) : top(_top) {
+    }
+    
+    virtual KDevelop::AbstractType::Ptr exchange(const KDevelop::AbstractType::Ptr& \
type) { +      if(!type)
+        return type;
+//       kDebug() << "simplifying" << type->toString();
+      
+      KDevelop::AbstractType::Ptr newType( type->clone() );
+      
+      if(const KDevelop::IdentifiedType* idType = dynamic_cast<const \
IdentifiedType*>(type.unsafeData())) { +        KDevelop::Declaration* decl = \
idType->declaration(top); +        if(TemplateDeclaration* tempDecl = \
dynamic_cast<TemplateDeclaration*>(decl))  +        {
+          TypeIdentifier newTypeName;
+          if(decl->context()->type() == DUContext::Class && \
decl->context()->owner()) { +            //Strip template default-parameters from the \
parent class +            AbstractType::Ptr parentType = \
stripTemplateDefaultParameters(decl->context()->owner()->abstractType(), top); +      \
if(parentType) +              newTypeName = TypeIdentifier(parentType->toString());
+          }
+          if(newTypeName.isEmpty())
+            newTypeName = decl->context()->scopeIdentifier(true);
+          
+          Identifier currentId;
+          if(!idType->qualifiedIdentifier().isEmpty())
+            currentId.setIdentifier(idType->qualifiedIdentifier().last().identifier().str());
 +          
+          KDevelop::InstantiationInformation instantiationInfo = \
tempDecl->instantiatedWith().information(); +          int neededParameters = 0;
+          KDevelop::InstantiationInformation newInformation(instantiationInfo);
+          newInformation.templateParametersList().clear();
+          
+          for(neededParameters = 0; neededParameters < \
instantiationInfo.templateParametersSize(); ++neededParameters) { +            \
newInformation.templateParametersList().append(instantiationInfo.templateParameters()[neededParameters]);
 +            AbstractType::Ptr niceParam = \
stripTemplateDefaultParameters(instantiationInfo.templateParameters()[neededParameters].abstractType(), \
top); +            if(niceParam) {
+              currentId.appendTemplateIdentifier(niceParam->toString());
+//               kDebug() << "testing param" << niceParam->toString();
+            }
+            
+            if(tempDecl->instantiate(newInformation, top) == decl) {
+//               kDebug() << "got full instantiation";
+              break;
+            }
+          }
+          
+          newTypeName.push(currentId);
+          
+          DelayedType::Ptr ret(new KDevelop::DelayedType);
+          ret->setIdentifier(newTypeName);
+//           kDebug() << "created delayed type" << ret->toString();
+          return ret.cast<AbstractType>();
+        }else{
+          return type;
+        }
+      }      
+      newType->exchangeTypes(this);
+      
+      return newType;
+    }
+  };
+  
+  ShortenTemplateDefaultParameter exchanger(top);
+  type = exchanger.exchange(type);
+  return type;
+}
 
-QString shortenedTypeString(Declaration* decl, int desiredLength, \
QualifiedIdentifier stripPrefix) { +QString \
shortenedTypeString(KDevelop::Declaration* decl, KDevelop::TopDUContext* top, int \
desiredLength, KDevelop::QualifiedIdentifier stripPrefix) {  AbstractType::Ptr type = \
decl->abstractType();  if(decl->isTypeAlias()) {
       if(type.cast<TypeAliasType>())
@@ -514,11 +590,16 @@
     type = funType->returnType();
   }
   
-  return shortenedTypeString(type);
+  return shortenedTypeString(type, top, desiredLength, stripPrefix);
 }
 
-QString shortenedTypeString(KDevelop::AbstractType::Ptr type, int desiredLength, \
KDevelop::QualifiedIdentifier stripPrefix) { +QString \
simplifiedTypeString(KDevelop::AbstractType::Ptr type, KDevelop::DUContext* \
visibilityFrom) { +  ///@todo Nicely respect the source visibility context
+  return shortenedTypeString(type, visibilityFrom ? visibilityFrom->topContext() : \
0, 100000, visibilityFrom ? visibilityFrom->scopeIdentifier(true) : \
QualifiedIdentifier()); +}
 
+QString shortenedTypeString(KDevelop::AbstractType::Ptr type, TopDUContext* top, int \
desiredLength, KDevelop::QualifiedIdentifier stripPrefix) { +
   bool isReference = false;
   if(type.cast<ReferenceType>()) {
     isReference = true;
@@ -527,9 +608,12 @@
 
   type = shortenTypeForViewing(type);
   
+  if(top)
+    type = stripTemplateDefaultParameters(type, top);
+  
   if(!type)
     return QString();
-
+  
   TypeIdentifier identifier = TypeIdentifier(type->toString());
   if(isReference)
     identifier.setIsReference(true);
--- trunk/KDE/kdevelop/languages/cpp/cppduchain/cppduchain.h #941563:941564
@@ -125,12 +125,15 @@
 ///Tries to un-typedef the given type using the uses directly before the given \
declaration.  KDEVCPPDUCHAIN_EXPORT KDevelop::TypeIdentifier \
unTypedefType(KDevelop::Declaration* decl, KDevelop::TypeIdentifier type);  
-///Returns a shortened string version of the type attached to the given declaration, \
using the uses to resolve typedefs and such. +///Returns a shortened string version \
of the type attached to the given declaration.  ///@param desiredLength the desired \
length. No guarantee that the resulting string will be this short. With the \
default-value, no shortening will happen in most cases.  ///@param stripPrefix this \
prefix will be stripped from qualified identifiers. This is useful to remove parts of \
                the current context.
-KDEVCPPDUCHAIN_EXPORT QString shortenedTypeString(KDevelop::Declaration* decl, int \
desiredLength = 10000, KDevelop::QualifiedIdentifier stripPrefix = \
                KDevelop::QualifiedIdentifier());
-KDEVCPPDUCHAIN_EXPORT QString shortenedTypeString(KDevelop::AbstractType::Ptr type, \
int desiredLength = 10000, KDevelop::QualifiedIdentifier stripPrefix = \
KDevelop::QualifiedIdentifier()); +KDEVCPPDUCHAIN_EXPORT QString \
shortenedTypeString(KDevelop::Declaration* decl, KDevelop::TopDUContext* top, int \
desiredLength = 10000, KDevelop::QualifiedIdentifier stripPrefix = \
KDevelop::QualifiedIdentifier()); +KDEVCPPDUCHAIN_EXPORT QString \
shortenedTypeString(KDevelop::AbstractType::Ptr type, KDevelop::TopDUContext* top, \
int desiredLength = 10000, KDevelop::QualifiedIdentifier stripPrefix = \
KDevelop::QualifiedIdentifier());  
+///Returns a simplified string version of the given type: Template \
default-parameters are stripped away, qualified identifiers are simplified so they \
are as short as possible, while staying visible from the given context. \
+KDEVCPPDUCHAIN_EXPORT QString simplifiedTypeString(KDevelop::AbstractType::Ptr type, \
KDevelop::DUContext* visibilityFrom); +
 KDEVCPPDUCHAIN_EXPORT bool isFriend(KDevelop::Declaration* _class, \
KDevelop::Declaration* _friend);  
 ///Shortens the type by resolving typedefs that are not useful
--- trunk/KDE/kdevelop/languages/cpp/cppduchain/missingdeclarationassistant.cpp \
#941563:941564 @@ -56,7 +56,7 @@
         QString typeString(int maxSize = 10000) const {
           DUChainReadLocker lock(DUChain::lock());
           if(DUContext* searchFrom = problem->type->searchStartContext.data())
-            return Cpp::shortenedTypeString(type(), maxSize, \
searchFrom->scopeIdentifier(true)); +            return \
Cpp::shortenedTypeString(type(), searchFrom->topContext(), maxSize, \
searchFrom->scopeIdentifier(true));  else
             return QString();
         }
@@ -145,7 +145,7 @@
           if(!type)
             return "<no type>";
           if(DUContext* container = useContainer())
-            return Cpp::shortenedTypeString(type, 30, \
container->scopeIdentifier(true)); +            return Cpp::shortenedTypeString(type, \
container->topContext(), 30, container->scopeIdentifier(true));  else
             return QString();
         }
--- trunk/KDE/kdevelop/languages/cpp/cppduchain/sourcemanipulation.cpp #941563:941564
@@ -199,7 +199,7 @@
     if(!ret.isEmpty())
       ret += ", ";
     AbstractType::Ptr type = TypeUtils::removeConstants(item.type);
-    ret += Cpp::shortenedTypeString(type, 100000, context->scopeIdentifier(true));
+    ret += Cpp::simplifiedTypeString(type, context);
     
     if(!item.name.isEmpty())
       ret += " " + item.name;
@@ -213,7 +213,7 @@
   
   returnType = TypeUtils::removeConstants(returnType);
   
-  QString decl = Cpp::shortenedTypeString(returnType, 100000, \
m_context->scopeIdentifier(true)) + " " + name.toString() + "(" + \
makeSignatureString(signature, m_context) + ")"; +  QString decl = \
Cpp::simplifiedTypeString(returnType, m_context) + " " + name.toString() + "(" + \
makeSignatureString(signature, m_context) + ")";  
   if(isConstant)
     decl += " const";
@@ -234,7 +234,7 @@
   
   type = TypeUtils::removeConstants(type);
   
-  QString decl = Cpp::shortenedTypeString(type, 100000, \
m_context->scopeIdentifier(true)) + " " + name.toString() + ";\n"; +  QString decl = \
Cpp::simplifiedTypeString(type, m_context) + " " + name.toString() + ";\n";  
   InsertionPoint insertion = findInsertionPoint(m_access, Variable);
   
@@ -315,7 +315,7 @@
         return false;
       }
       
-      forwardDeclaration = "typedef " + decl->abstractType()->toString() + " " + \
decl->identifier().toString() + ";\n"; +      forwardDeclaration = "typedef " + \
Cpp::simplifiedTypeString(decl->abstractType(), m_context) + " " + \
decl->identifier().toString() + ";\n";  }else{
       DUContext* templateContext = getTemplateContext(decl);
       if(templateContext) {
@@ -335,7 +335,7 @@
           if(templParamType) {
             forwardDeclaration += "class ";
           }else if(paramDecl->abstractType()) {
-            forwardDeclaration += paramDecl->abstractType()->toString() + " ";
+            forwardDeclaration += \
Cpp::simplifiedTypeString(paramDecl->abstractType(), m_context) + " ";  }
           
           forwardDeclaration += paramDecl->identifier().toString();
--- trunk/KDE/kdevelop/languages/cpp/cppduchain/tests/test_duchain.cpp #941563:941564
@@ -2817,10 +2817,10 @@
   AbstractType::Ptr argType = \
top->childContexts()[1]->localDeclarations()[0]->abstractType();  \
QVERIFY(argType.cast<ReferenceType>());  QCOMPARE(argType->toString().remove(' '), \
                QString("CC<constA*>&"));
-  QCOMPARE(Cpp::shortenedTypeString(top->childContexts()[1]->localDeclarations()[0], \
10000).remove(' '), QString("CC<constA*>&")); +  \
QCOMPARE(Cpp::shortenedTypeString(top->childContexts()[1]->localDeclarations()[0], \
top, 10000).remove(' '), QString("CC<constA*>&"));  \
                QVERIFY(top->localDeclarations()[3]->abstractType());
-  QCOMPARE(Cpp::shortenedTypeString(top->localDeclarations()[3], 10000).remove(' '), \
                QString("constA&"));
-  QCOMPARE(Cpp::shortenedTypeString(top->localDeclarations()[4], 10000).remove(' '), \
QString("constA***")); +  \
QCOMPARE(Cpp::shortenedTypeString(top->localDeclarations()[3], top, 10000).remove(' \
'), QString("constA&")); +  \
QCOMPARE(Cpp::shortenedTypeString(top->localDeclarations()[4], top, 10000).remove(' \
'), QString("constA***"));  }
 
 void TestDUChain::testTemplates() {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic