[cfe-commits] [PATCH (need approval)] Add instantiations of out-of-line member variable declarations to DeclContext

Peter Collingbourne peter at pcc.me.uk
Sun Jul 4 07:02:35 PDT 2010


Currently instantiations of out-of-line member variable declarations
are created and added to the redeclaration list but are not added to
any DeclContext.  This is a problem for any traversal of the AST which
depends on seeing identical sets of AST nodes via the redeclaration
lists and via DeclContexts.

This patch causes such variable declarations to be added to the lexical
DeclContext.
---
 lib/Sema/SemaTemplateInstantiateDecl.cpp           |    1 +
 .../SemaCXX/out-of-line-variable-instantiation.cpp |   19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)
 create mode 100644 test/SemaCXX/out-of-line-variable-instantiation.cpp

diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 06f38e8..120ae9d 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2245,6 +2245,7 @@ void Sema::InstantiateStaticDataMemberDefinition(
                                        MSInfo->getPointOfInstantiation());
     DeclGroupRef DG(Var);
     Consumer.HandleTopLevelDecl(DG);
+    Var->getLexicalDeclContext()->addDecl(Var);
   }
 
   if (Recursive) {
diff --git a/test/SemaCXX/out-of-line-variable-instantiation.cpp b/test/SemaCXX/out-of-line-variable-instantiation.cpp
new file mode 100644
index 0000000..6aa9830
--- /dev/null
+++ b/test/SemaCXX/out-of-line-variable-instantiation.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -ast-print-xml -o - %s | FileCheck %s
+
+template<typename T> struct foo {
+  static const T m = 0;
+};
+
+template<typename T> const T foo<T>::m;
+const int i = foo<int>::m;
+
+// CHECK: <TranslationUnit>
+// The first declaration is the template
+// CHECK: <Var{{.*}} name="m"
+// The second declaration is the instantiation
+// CHECK: <Var{{.*}} context="[[CONTEXT:[^"]+]]"{{.*}} name="m"
+// CHECK: </TranslationUnit>
+
+// CHECK: <Contexts>
+// CHECK: <ClassTemplateSpecialization{{.*}} id="[[CONTEXT]]"
+// CHECK: </Contexts>
-- 
1.6.5




More information about the cfe-commits mailing list