From 6c9c94053132e5ca0655124b70f1c386a332e71d Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Sat, 30 May 2009 06:48:27 +0000 Subject: [PATCH] Pretty printing and improved representation for namespace alias declarations git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72616 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclCXX.h | 27 +++++++++++++++++++++++-- lib/AST/DeclCXX.cpp | 6 ++++-- lib/AST/DeclPrinter.cpp | 8 ++++++++ lib/Sema/SemaDeclCXX.cpp | 4 +++- test/Coverage/cxx-language-features.inc | 2 ++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h index 7a5233a6af..0971b6900e 100644 --- a/include/clang/AST/DeclCXX.h +++ b/include/clang/AST/DeclCXX.h @@ -970,9 +970,16 @@ public: /// @endcode class NamespaceAliasDecl : public NamedDecl { SourceLocation AliasLoc; + + /// \brief The source range that covers the nested-name-specifier + /// preceding the namespace name. + SourceRange QualifierRange; + + /// \brief The nested-name-specifier that precedes the namespace + /// name, if any. + NestedNameSpecifier *Qualifier; /// IdentLoc - Location of namespace identifier. - /// FIXME: We don't store location of scope specifier. SourceLocation IdentLoc; /// Namespace - The Decl that this alias points to. Can either be a @@ -981,11 +988,21 @@ class NamespaceAliasDecl : public NamedDecl { NamespaceAliasDecl(DeclContext *DC, SourceLocation L, SourceLocation AliasLoc, IdentifierInfo *Alias, + SourceRange QualifierRange, + NestedNameSpecifier *Qualifier, SourceLocation IdentLoc, NamedDecl *Namespace) : NamedDecl(Decl::NamespaceAlias, DC, L, Alias), AliasLoc(AliasLoc), + QualifierRange(QualifierRange), Qualifier(Qualifier), IdentLoc(IdentLoc), Namespace(Namespace) { } public: + /// \brief Retrieve the source range of the nested-name-specifier + /// that qualifiers the namespace name. + SourceRange getQualifierRange() const { return QualifierRange; } + + /// \brief Retrieve the nested-name-specifier that qualifies the + /// name of the namespace. + NestedNameSpecifier *getQualifier() const { return Qualifier; } NamespaceDecl *getNamespace() { if (NamespaceAliasDecl *AD = dyn_cast(Namespace)) @@ -997,10 +1014,16 @@ public: const NamespaceDecl *getNamespace() const { return const_cast(this)->getNamespace(); } - + + /// \brief Retrieve the namespace that this alias refers to, which + /// may either be a NamespaceDecl or a NamespaceAliasDecl. + NamedDecl *getAliasedNamespace() const { return Namespace; } + static NamespaceAliasDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, SourceLocation AliasLoc, IdentifierInfo *Alias, + SourceRange QualifierRange, + NestedNameSpecifier *Qualifier, SourceLocation IdentLoc, NamedDecl *Namespace); diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp index 5fb2a016b6..6354d84784 100644 --- a/lib/AST/DeclCXX.cpp +++ b/lib/AST/DeclCXX.cpp @@ -415,10 +415,12 @@ NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, SourceLocation AliasLoc, IdentifierInfo *Alias, + SourceRange QualifierRange, + NestedNameSpecifier *Qualifier, SourceLocation IdentLoc, NamedDecl *Namespace) { - return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, IdentLoc, - Namespace); + return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, + Qualifier, IdentLoc, Namespace); } StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index 01cbee4d63..2858c0a201 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -55,6 +55,7 @@ namespace { void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); void VisitOverloadedFunctionDecl(OverloadedFunctionDecl *D); void VisitUsingDirectiveDecl(UsingDirectiveDecl *D); + void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); void VisitNamespaceDecl(NamespaceDecl *D); void VisitLinkageSpecDecl(LinkageSpecDecl *D); void VisitTemplateDecl(TemplateDecl *D); @@ -435,6 +436,13 @@ void DeclPrinter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { Out << D->getNominatedNamespace()->getNameAsString(); } +void DeclPrinter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { + Out << "namespace " << D->getNameAsString() << " = "; + if (D->getQualifier()) + D->getQualifier()->print(Out, Policy); + Out << D->getAliasedNamespace()->getNameAsString(); +} + void DeclPrinter::VisitNamespaceDecl(NamespaceDecl *D) { Out << "namespace " << D->getNameAsString() << " {\n"; VisitDeclContext(D); diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index cd722fe169..67c6dc3fae 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -1787,7 +1787,9 @@ Sema::DeclPtrTy Sema::ActOnNamespaceAliasDef(Scope *S, } NamespaceAliasDecl *AliasDecl = - NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, Alias, + NamespaceAliasDecl::Create(Context, CurContext, NamespaceLoc, AliasLoc, + Alias, SS.getRange(), + (NestedNameSpecifier *)SS.getScopeRep(), IdentLoc, R); CurContext->addDecl(Context, AliasDecl); diff --git a/test/Coverage/cxx-language-features.inc b/test/Coverage/cxx-language-features.inc index 92c9fd6f27..bac6bc7910 100644 --- a/test/Coverage/cxx-language-features.inc +++ b/test/Coverage/cxx-language-features.inc @@ -10,3 +10,5 @@ namespace std { using namespace std::debug; using namespace std; + +namespace safestl = ::std::debug; -- 2.40.0