[cfe-commits] r67966 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaCXXScopeSpec.cpp lib/Sema/SemaDeclCXX.cpp test/SemaCXX/namespace-alias.cpp

Anders Carlsson andersca at mac.com
Sat Mar 28 16:53:49 PDT 2009


Author: andersca
Date: Sat Mar 28 18:53:49 2009
New Revision: 67966

URL: http://llvm.org/viewvc/llvm-project?rev=67966&view=rev
Log:
More improvements to namespace aliases. We now support everything except aliases in using directives.

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaCXXScopeSpec.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/namespace-alias.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=67966&r1=67965&r2=67966&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat Mar 28 18:53:49 2009
@@ -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 && 

Modified: cfe/trunk/lib/Sema/SemaCXXScopeSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXScopeSpec.cpp?rev=67966&r1=67965&r2=67966&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXScopeSpec.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXScopeSpec.cpp Sat Mar 28 18:53:49 2009
@@ -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.

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=67966&r1=67965&r2=67966&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Sat Mar 28 18:53:49 2009
@@ -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();

Modified: cfe/trunk/test/SemaCXX/namespace-alias.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/namespace-alias.cpp?rev=67966&r1=67965&r2=67966&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/namespace-alias.cpp (original)
+++ cfe/trunk/test/SemaCXX/namespace-alias.cpp Sat Mar 28 18:53:49 2009
@@ -25,3 +25,26 @@
 namespace G { 
   namespace B = N;
 }
+
+namespace H {
+  namespace A1 { }
+  namespace A2 { }
+
+  // These all point to A1.
+  namespace B = A1; // expected-note {{previous definition is here}}
+  namespace B = A1;
+  namespace C = B;
+  namespace B = C;
+
+  namespace B = A2; // expected-error {{redefinition of 'B' as different kind of symbol}}
+}
+
+namespace I { 
+  namespace A1 { int i; }
+  
+  namespace A2 = A1;
+}
+
+int f() {
+  return I::A2::i;
+}





More information about the cfe-commits mailing list