[cfe-commits] r89657 - in /cfe/trunk: include/clang/AST/DeclCXX.h lib/AST/DeclCXX.cpp lib/AST/DeclPrinter.cpp lib/Sema/SemaDeclCXX.cpp test/SemaCXX/using-directive.cpp

Sebastian Redl sebastian.redl at getdesigned.at
Mon Nov 23 07:34:24 PST 2009


Author: cornedbee
Date: Mon Nov 23 09:34:23 2009
New Revision: 89657

URL: http://llvm.org/viewvc/llvm-project?rev=89657&view=rev
Log:
Let using directives refer to namespace aliases. Fixes PR5479.

Modified:
    cfe/trunk/include/clang/AST/DeclCXX.h
    cfe/trunk/lib/AST/DeclCXX.cpp
    cfe/trunk/lib/AST/DeclPrinter.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/using-directive.cpp

Modified: cfe/trunk/include/clang/AST/DeclCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclCXX.h?rev=89657&r1=89656&r2=89657&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclCXX.h (original)
+++ cfe/trunk/include/clang/AST/DeclCXX.h Mon Nov 23 09:34:23 2009
@@ -1514,9 +1514,9 @@
   SourceLocation IdentLoc;
 
   /// NominatedNamespace - Namespace nominated by using-directive.
-  NamespaceDecl *NominatedNamespace;
+  NamedDecl *NominatedNamespace;
 
-  /// Enclosing context containing both using-directive and nomintated
+  /// Enclosing context containing both using-directive and nominated
   /// namespace.
   DeclContext *CommonAncestor;
 
@@ -1532,12 +1532,12 @@
                      SourceRange QualifierRange,
                      NestedNameSpecifier *Qualifier,
                      SourceLocation IdentLoc,
-                     NamespaceDecl *Nominated,
+                     NamedDecl *Nominated,
                      DeclContext *CommonAncestor)
     : NamedDecl(Decl::UsingDirective, DC, L, getName()),
       NamespaceLoc(NamespcLoc), QualifierRange(QualifierRange),
       Qualifier(Qualifier), IdentLoc(IdentLoc),
-      NominatedNamespace(Nominated? Nominated->getOriginalNamespace() : 0),
+      NominatedNamespace(Nominated),
       CommonAncestor(CommonAncestor) {
   }
 
@@ -1550,8 +1550,13 @@
   /// name of the namespace.
   NestedNameSpecifier *getQualifier() const { return Qualifier; }
 
+  NamedDecl *getNominatedNamespaceAsWritten() { return NominatedNamespace; }
+  const NamedDecl *getNominatedNamespaceAsWritten() const {
+    return NominatedNamespace;
+  }
+
   /// getNominatedNamespace - Returns namespace nominated by using-directive.
-  NamespaceDecl *getNominatedNamespace() { return NominatedNamespace; }
+  NamespaceDecl *getNominatedNamespace();
 
   const NamespaceDecl *getNominatedNamespace() const {
     return const_cast<UsingDirectiveDecl*>(this)->getNominatedNamespace();
@@ -1574,7 +1579,7 @@
                                     SourceRange QualifierRange,
                                     NestedNameSpecifier *Qualifier,
                                     SourceLocation IdentLoc,
-                                    NamespaceDecl *Nominated,
+                                    NamedDecl *Nominated,
                                     DeclContext *CommonAncestor);
 
   static bool classof(const Decl *D) {

Modified: cfe/trunk/lib/AST/DeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclCXX.cpp?rev=89657&r1=89656&r2=89657&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclCXX.cpp (original)
+++ cfe/trunk/lib/AST/DeclCXX.cpp Mon Nov 23 09:34:23 2009
@@ -886,12 +886,21 @@
                                                SourceRange QualifierRange,
                                                NestedNameSpecifier *Qualifier,
                                                SourceLocation IdentLoc,
-                                               NamespaceDecl *Used,
+                                               NamedDecl *Used,
                                                DeclContext *CommonAncestor) {
+  if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used))
+    Used = NS->getOriginalNamespace();
   return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange,
                                     Qualifier, IdentLoc, Used, CommonAncestor);
 }
 
+NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() {
+  if (NamespaceAliasDecl *NA =
+        dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace))
+    return NA->getNamespace();
+  return cast_or_null<NamespaceDecl>(NominatedNamespace);
+}
+
 NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC,
                                                SourceLocation L,
                                                SourceLocation AliasLoc,
@@ -900,6 +909,8 @@
                                                NestedNameSpecifier *Qualifier,
                                                SourceLocation IdentLoc,
                                                NamedDecl *Namespace) {
+  if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace))
+    Namespace = NS->getOriginalNamespace();
   return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange,
                                     Qualifier, IdentLoc, Namespace);
 }

Modified: cfe/trunk/lib/AST/DeclPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclPrinter.cpp?rev=89657&r1=89656&r2=89657&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclPrinter.cpp (original)
+++ cfe/trunk/lib/AST/DeclPrinter.cpp Mon Nov 23 09:34:23 2009
@@ -505,7 +505,7 @@
   Out << "using namespace ";
   if (D->getQualifier())
     D->getQualifier()->print(Out, Policy);
-  Out << D->getNominatedNamespace()->getNameAsString();
+  Out << D->getNominatedNamespaceAsWritten()->getNameAsString();
 }
 
 void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Nov 23 09:34:23 2009
@@ -2722,6 +2722,14 @@
   return DeclPtrTy::make(Namespc);
 }
 
+/// 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);
+}
+
 /// ActOnFinishNamespaceDef - This callback is called after a namespace is
 /// exited. Decl is the DeclTy returned by ActOnStartNamespaceDef.
 void Sema::ActOnFinishNamespaceDef(DeclPtrTy D, SourceLocation RBrace) {
@@ -2753,9 +2761,9 @@
     return DeclPtrTy();
 
   if (!R.empty()) {
-    NamedDecl *NS = R.getFoundDecl();
-    // FIXME: Namespace aliases!
-    assert(isa<NamespaceDecl>(NS) && "expected namespace decl");
+    NamedDecl *Named = R.getFoundDecl();
+    assert((isa<NamespaceDecl>(Named) || isa<NamespaceAliasDecl>(Named))
+        && "expected namespace decl");
     // C++ [namespace.udir]p1:
     //   A using-directive specifies that the names in the nominated
     //   namespace can be used in the scope in which the
@@ -2768,18 +2776,15 @@
 
     // Find enclosing context containing both using-directive and
     // nominated namespace.
+    NamespaceDecl *NS = getNamespaceDecl(Named);
     DeclContext *CommonAncestor = cast<DeclContext>(NS);
     while (CommonAncestor && !CommonAncestor->Encloses(CurContext))
       CommonAncestor = CommonAncestor->getParent();
 
-    UDir = UsingDirectiveDecl::Create(Context,
-                                      CurContext, UsingLoc,
-                                      NamespcLoc,
+    UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc,
                                       SS.getRange(),
                                       (NestedNameSpecifier *)SS.getScopeRep(),
-                                      IdentLoc,
-                                      cast<NamespaceDecl>(NS),
-                                      CommonAncestor);
+                                      IdentLoc, Named, CommonAncestor);
     PushUsingDirective(S, UDir);
   } else {
     Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange();
@@ -3006,14 +3011,6 @@
   return UD;
 }
 
-/// 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,

Modified: cfe/trunk/test/SemaCXX/using-directive.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/using-directive.cpp?rev=89657&r1=89656&r2=89657&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/using-directive.cpp (original)
+++ cfe/trunk/test/SemaCXX/using-directive.cpp Mon Nov 23 09:34:23 2009
@@ -102,3 +102,13 @@
     (void)X(); // expected-error{{reference to 'X' is ambiguous}}
   }
 }
+
+// PR5479
+namespace Aliased {
+  void inAliased();
+}
+namespace Alias = Aliased;
+using namespace Alias;
+void testAlias() {
+  inAliased();
+}





More information about the cfe-commits mailing list