More improvements to namespace aliases. We now support everything except aliases in using directives.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67966 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index ab1e9d4..0ebccf3 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -970,7 +970,7 @@
       return isa<TypedefDecl>(D) || D->isInIdentifierNamespace(Decl::IDNS_Tag);
       
     case Sema::LookupNamespaceName:
-      return isa<NamespaceDecl>(D);
+      return isa<NamespaceDecl>(D) || isa<NamespaceAliasDecl>(D);
     }
     
     assert(false && 
diff --git a/lib/Sema/SemaCXXScopeSpec.cpp b/lib/Sema/SemaCXXScopeSpec.cpp
index a879989..5395869 100644
--- a/lib/Sema/SemaCXXScopeSpec.cpp
+++ b/lib/Sema/SemaCXXScopeSpec.cpp
@@ -144,6 +144,10 @@
         return NestedNameSpecifier::Create(Context, Prefix, false, 
                                            T.getTypePtr());
     }
+    
+    if (NamespaceAliasDecl *Alias = dyn_cast<NamespaceAliasDecl>(SD))
+      return NestedNameSpecifier::Create(Context, Prefix,
+                                         Alias->getNamespace());
 
     // Fall through to produce an error: we found something that isn't
     // a class or a namespace.
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index 300e7c4..22e773a 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -1676,6 +1676,14 @@
     S->PushUsingDirective(DeclPtrTy::make(UDir));
 }
 
+/// getNamespaceDecl - Returns the namespace a decl represents. If the decl
+/// is a namespace alias, returns the namespace it points to.
+static inline NamespaceDecl *getNamespaceDecl(NamedDecl *D) {
+  if (NamespaceAliasDecl *AD = dyn_cast_or_null<NamespaceAliasDecl>(D))
+    return AD->getNamespace();
+  return dyn_cast_or_null<NamespaceDecl>(D);
+}
+
 Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, 
                                              SourceLocation NamespaceLoc,
                                              SourceLocation AliasLoc,
@@ -1684,10 +1692,18 @@
                                              SourceLocation IdentLoc,
                                              IdentifierInfo *Ident) {
   
+  // Lookup the namespace name.
+  LookupResult R = LookupParsedName(S, &SS, Ident, LookupNamespaceName, false);
+
   // Check if we have a previous declaration with the same name.
   if (NamedDecl *PrevDecl = LookupName(S, Alias, LookupOrdinaryName, true)) {
-    // FIXME: If this is a namespace alias decl, and it points to the same 
-    // namespace, we shouldn't warn.
+    if (NamespaceAliasDecl *AD = dyn_cast<NamespaceAliasDecl>(PrevDecl)) {
+      // We already have an alias with the same name that points to the same 
+      // namespace, so don't create a new one.
+      if (!R.isAmbiguous() && AD->getNamespace() == getNamespaceDecl(R))
+        return DeclPtrTy();
+    }
+    
     unsigned DiagID = isa<NamespaceDecl>(PrevDecl) ? diag::err_redefinition :
       diag::err_redefinition_different_kind;
     Diag(AliasLoc, DiagID) << Alias;
@@ -1695,8 +1711,6 @@
     return DeclPtrTy();
   }
 
-  // Lookup the namespace name.
-  LookupResult R = LookupParsedName(S, &SS, Ident, LookupNamespaceName, false);
   if (R.isAmbiguous()) {
     DiagnoseAmbiguousLookup(R, Ident, IdentLoc);
     return DeclPtrTy();