When instantiating a function-scoped enum, make sure that it and its
enumeration constants get placed into the local instantiation hash
table. Fixes PR6375.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@97471 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index da192dd..4d017ca 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -3433,7 +3433,7 @@
     
     void InstantiatedLocal(const Decl *D, Decl *Inst) {
       Decl *&Stored = LocalDecls[D];
-      assert(!Stored && "Already instantiated this local");
+      assert((!Stored || Stored == Inst) && "Already instantiated this local");
       Stored = Inst;
     }
   };
diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 0f7bae8..292820b 100644
--- a/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -491,6 +491,9 @@
   Owner->addDecl(Enum);
   Enum->startDefinition();
 
+  if (D->getDeclContext()->isFunctionOrMethod())
+    SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum);
+    
   llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators;
 
   EnumConstantDecl *LastEnumConst = 0;
@@ -530,6 +533,12 @@
       Enum->addDecl(EnumConst);
       Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst));
       LastEnumConst = EnumConst;
+      
+      if (D->getDeclContext()->isFunctionOrMethod()) {
+        // If the enumeration is within a function or method, record the enum
+        // constant as a local.
+        SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst);
+      }
     }
   }
 
diff --git a/test/SemaTemplate/instantiate-enum.cpp b/test/SemaTemplate/instantiate-enum.cpp
index 6f9aa4b..5353a92 100644
--- a/test/SemaTemplate/instantiate-enum.cpp
+++ b/test/SemaTemplate/instantiate-enum.cpp
@@ -9,3 +9,19 @@
 };
 
 int array1[adder<long, 3, 4>::value == 7? 1 : -1];
+
+namespace PR6375 {
+  template<typename T> 
+  void f() {
+    enum Enum
+    {
+      enumerator1 = 0xFFFFFFF,
+      enumerator2 = enumerator1 - 1
+    };
+  
+    int xb1 = enumerator1;
+    int xe1 = enumerator2;
+  }
+
+  template void f<int>();
+}