From 5d3c0701741edb64aae3b7281e78cb3f9521a6fe Mon Sep 17 00:00:00 2001
From: Przemyslaw Gorszkowski <pgorszkowski@gmail.com>
Date: Tue, 25 Mar 2014 09:23:31 -0400
Subject: [PATCH] C++: Do not set binding block as instantiationOrigin

The binding in a block should not be used as an instantiationOrigin,
because it will be deleted if it does not contain any blocks or
namespaces or classes or enums.
Instead of that we should look for first enclosing class or namespace.

Task-number: QTCREATORBUG-11424
Change-Id: I0fc6e935495478f71372b0fe6f611887f45b2eda
Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
---
 src/libs/cplusplus/LookupContext.cpp        |  4 ++++
 src/plugins/cpptools/cppcompletion_test.cpp | 16 ++++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/src/libs/cplusplus/LookupContext.cpp b/src/libs/cplusplus/LookupContext.cpp
index 0d600993c68..b86c295f931 100644
--- a/src/libs/cplusplus/LookupContext.cpp
+++ b/src/libs/cplusplus/LookupContext.cpp
@@ -1069,6 +1069,10 @@ ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespac
         instantiation->_name = templId;
 #endif // DEBUG_LOOKUP
         instantiation->_templateId = templId;
+
+        while (!origin->_symbols.isEmpty() && origin->_symbols[0]->isBlock())
+            origin = origin->parent();
+
         instantiation->_instantiationOrigin = origin;
 
         // The instantiation should have all symbols, enums, and usings from the reference.
diff --git a/src/plugins/cpptools/cppcompletion_test.cpp b/src/plugins/cpptools/cppcompletion_test.cpp
index eb657f783b1..742e0a99eee 100644
--- a/src/plugins/cpptools/cppcompletion_test.cpp
+++ b/src/plugins/cpptools/cppcompletion_test.cpp
@@ -2190,6 +2190,22 @@ void CppToolsPlugin::test_completion_data()
             "}\n"
         ) << _("mem") << (QStringList()
             << QLatin1String("member"));
+
+    QTest::newRow("no_binding_block_as_instantiationOrigin_QTCREATORBUG-11424") << _(
+            "template <typename T>\n"
+            "class QVector\n"
+            "{\n"
+            "public:\n"
+            "   inline const_iterator constBegin() const;\n"
+            "};\n"
+            "\n"
+            "typedef struct { double value; } V;\n"
+            "\n"
+            "double getValue(const QVector<V>& d) const {\n"
+            "   typedef QVector<V>::ConstIterator Iter;\n"
+            "   double val = @\n"
+            "}\n"
+        ) << _("d.constBegin()->") << (QStringList());
 }
 
 void CppToolsPlugin::test_completion_member_access_operator()
-- 
GitLab