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

List:       kde-commits
Subject:    [kdev-php] duchain: parse @var typehint for any variable declaration
From:       Niko Sams <niko.sams () gmail ! com>
Date:       2013-11-17 13:14:41
Message-ID: E1Vi2BV-0003I1-NU () scm ! kde ! org
[Download RAW message or body]

Git commit 18a8de6ea637cd2db9e8d3ed104b8d1710b90765 by Niko Sams.
Committed on 17/11/2013 at 12:43.
Pushed by nsams into branch 'master'.

parse @var typehint for any variable declaration

this overrides our expression parser similar to @return typehints

M  +11   -7    duchain/builders/typebuilder.cpp
M  +22   -0    duchain/tests/duchain.cpp
M  +1    -0    duchain/tests/duchain.h

http://commits.kde.org/kdev-php/18a8de6ea637cd2db9e8d3ed104b8d1710b90765

diff --git a/duchain/builders/typebuilder.cpp b/duchain/builders/typebuilder.cpp
index 296f782..2d3e8fc 100644
--- a/duchain/builders/typebuilder.cpp
+++ b/duchain/builders/typebuilder.cpp
@@ -239,16 +239,20 @@ QList<AbstractType::Ptr> \
TypeBuilder::parseDocCommentParams(AstNode* node)  
 AbstractType::Ptr TypeBuilder::getTypeForNode(AstNode* node)
 {
+
     AbstractType::Ptr type;
     if (node) {
-        node->ducontext = currentContext();
-        ExpressionParser ep;
-        ep.setCreateProblems(true);
-        ExpressionEvaluationResult res = ep.evaluateType(node, editor());
-        if (res.hadUnresolvedIdentifiers()) {
-            m_hadUnresolvedIdentifiers = true;
+        type = parseDocComment(node, "var"); //we fully trust in @var typehint and \
don't try to evaluate ourself +        if (!type) {
+            node->ducontext = currentContext();
+            ExpressionParser ep(true);
+            ep.setCreateProblems(true);
+            ExpressionEvaluationResult res = ep.evaluateType(node, editor());
+            if (res.hadUnresolvedIdentifiers()) {
+                m_hadUnresolvedIdentifiers = true;
+            }
+            type = res.type();
         }
-        type = res.type();
     }
     if (!type) {
         type = AbstractType::Ptr(new IntegralType(IntegralType::TypeMixed));
diff --git a/duchain/tests/duchain.cpp b/duchain/tests/duchain.cpp
index 52fb9fe..7124f17 100644
--- a/duchain/tests/duchain.cpp
+++ b/duchain/tests/duchain.cpp
@@ -135,6 +135,28 @@ void TestDUChain::declareVar()
     QVERIFY(decVar->type<IntegralType>());
 }
 
+void TestDUChain::varTypehint()
+{
+    //                 0         1         2         3         4         5         6 \
7 +    //                 \
01234567890123456789012345678901234567890123456789012345678901234567890123456789 +    \
QByteArray method("<? class A {} /** @var A **/ $i = foo();"); +
+    TopDUContext* top = parse(method, DumpAll);
+    DUChainReleaser releaseTop(top);
+    DUChainWriteLocker lock(DUChain::lock());
+
+    //class A
+    Declaration* dec = top->localDeclarations().at(0);
+
+    //$i
+    Declaration* decVar = top->localDeclarations().at(1);
+    QCOMPARE(decVar->identifier(), Identifier("i"));
+    StructureType::Ptr classType = decVar->type<StructureType>();
+    QVERIFY(classType);
+    QCOMPARE(classType->qualifiedIdentifier(), QualifiedIdentifier("a"));
+    QVERIFY(classType->equals(dec->abstractType().unsafeData()));
+}
+
 void TestDUChain::declareClass()
 {
     //                 0         1         2         3         4         5         6 \
                7
diff --git a/duchain/tests/duchain.h b/duchain/tests/duchain.h
index b227f6f..db276c5 100644
--- a/duchain/tests/duchain.h
+++ b/duchain/tests/duchain.h
@@ -33,6 +33,7 @@ public:
 private slots:
     void declareFunction();
     void declareVar();
+    void varTypehint();
     void declareClass();
     void classMemberVar();
     void declareTypehintFunction();


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

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