diff options
| author | Douglas Gregor <dgregor@apple.com> | 2010-08-31 23:48:11 +0000 |
|---|---|---|
| committer | Douglas Gregor <dgregor@apple.com> | 2010-08-31 23:48:11 +0000 |
| commit | a89314e396bda8b7c0f9b3e7a9d9a014b7beef0c (patch) | |
| tree | 4cc7ea5e8e28aa730b5b46dbf8b73593e39684f7 /clang/tools | |
| parent | 52bd0dc3bb7d3e1a30c38d3ec7e5c33e49ff3080 (diff) | |
| download | bcm5719-llvm-a89314e396bda8b7c0f9b3e7a9d9a014b7beef0c.tar.gz bcm5719-llvm-a89314e396bda8b7c0f9b3e7a9d9a014b7beef0c.zip | |
Add libclang support for namespace aliases (visitation + USRs) along
with a new cursor kind for a reference to a namespace.
There's still some oddities in the source location information for
NamespaceAliasDecl that I'll address with a separate commit, so the
source locations displayed in the load-namespaces.cpp test will
change.
llvm-svn: 112676
Diffstat (limited to 'clang/tools')
| -rw-r--r-- | clang/tools/libclang/CIndex.cpp | 32 | ||||
| -rw-r--r-- | clang/tools/libclang/CIndexUSRs.cpp | 6 | ||||
| -rw-r--r-- | clang/tools/libclang/CXCursor.cpp | 28 | ||||
| -rw-r--r-- | clang/tools/libclang/CXCursor.h | 10 |
4 files changed, 70 insertions, 6 deletions
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 2a7e6e9b065..230631bad9a 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -316,6 +316,7 @@ public: bool VisitObjCClassDecl(ObjCClassDecl *D); bool VisitLinkageSpecDecl(LinkageSpecDecl *D); bool VisitNamespaceDecl(NamespaceDecl *D); + bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D); // Name visitor bool VisitDeclarationNameInfo(DeclarationNameInfo Name); @@ -863,6 +864,13 @@ bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { return VisitDeclContext(D); } +bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { + // FIXME: Visit nested-name-specifier + + return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), + D->getTargetNameLoc(), TU)); +} + bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { switch (Name.getName().getNameKind()) { case clang::DeclarationName::Identifier: @@ -2053,10 +2061,17 @@ CXString clang_getCursorSpelling(CXCursor C) { } case CXCursor_TemplateRef: { TemplateDecl *Template = getCursorTemplateRef(C).first; - assert(Template && "Missing type decl"); + assert(Template && "Missing template decl"); return createCXString(Template->getNameAsString()); } + + case CXCursor_NamespaceRef: { + NamedDecl *NS = getCursorNamespaceRef(C).first; + assert(NS && "Missing namespace decl"); + + return createCXString(NS->getNameAsString()); + } default: return createCXString("<not implemented>"); @@ -2138,6 +2153,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { return createCXString("TypeRef"); case CXCursor_TemplateRef: return createCXString("TemplateRef"); + case CXCursor_NamespaceRef: + return createCXString("NamespaceRef"); case CXCursor_UnexposedExpr: return createCXString("UnexposedExpr"); case CXCursor_BlockExpr: @@ -2200,6 +2217,8 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { return createCXString("ClassTemplate"); case CXCursor_ClassTemplatePartialSpecialization: return createCXString("ClassTemplatePartialSpecialization"); + case CXCursor_NamespaceAlias: + return createCXString("NamespaceAlias"); } llvm_unreachable("Unhandled CXCursorKind"); @@ -2329,6 +2348,11 @@ CXSourceLocation clang_getCursorLocation(CXCursor C) { return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); } + case CXCursor_NamespaceRef: { + std::pair<NamedDecl *, SourceLocation> P = getCursorNamespaceRef(C); + return cxloc::translateSourceLocation(P.first->getASTContext(), P.second); + } + case CXCursor_CXXBaseSpecifier: { // FIXME: Figure out what location to return for a CXXBaseSpecifier. return clang_getNullLocation(); @@ -2390,6 +2414,9 @@ static SourceRange getRawCursorExtent(CXCursor C) { case CXCursor_TemplateRef: return getCursorTemplateRef(C).second; + case CXCursor_NamespaceRef: + return getCursorNamespaceRef(C).second; + case CXCursor_CXXBaseSpecifier: // FIXME: Figure out what source range to use for a CXBaseSpecifier. return SourceRange(); @@ -2470,6 +2497,9 @@ CXCursor clang_getCursorReferenced(CXCursor C) { case CXCursor_TemplateRef: return MakeCXCursor(getCursorTemplateRef(C).first, CXXUnit); + case CXCursor_NamespaceRef: + return MakeCXCursor(getCursorNamespaceRef(C).first, CXXUnit); + case CXCursor_CXXBaseSpecifier: { CXXBaseSpecifier *B = cxcursor::getCursorCXXBaseSpecifier(C); return clang_getTypeDeclaration(cxtype::MakeCXType(B->getType(), diff --git a/clang/tools/libclang/CIndexUSRs.cpp b/clang/tools/libclang/CIndexUSRs.cpp index 6705076c9f0..9ef851aeaa2 100644 --- a/clang/tools/libclang/CIndexUSRs.cpp +++ b/clang/tools/libclang/CIndexUSRs.cpp @@ -65,6 +65,7 @@ public: void VisitFunctionDecl(FunctionDecl *D); void VisitNamedDecl(NamedDecl *D); void VisitNamespaceDecl(NamespaceDecl *D); + void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); void VisitClassTemplateDecl(ClassTemplateDecl *D); void VisitObjCClassDecl(ObjCClassDecl *CD); @@ -257,6 +258,11 @@ void USRGenerator::VisitClassTemplateDecl(ClassTemplateDecl *D) { VisitTagDecl(D->getTemplatedDecl()); } +void USRGenerator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { + VisitDeclContext(D->getDeclContext()); + if (!IgnoreResults) + Out << "@NA@" << D->getName(); +} void USRGenerator::VisitObjCMethodDecl(ObjCMethodDecl *D) { Decl *container = cast<Decl>(D->getDeclContext()); diff --git a/clang/tools/libclang/CXCursor.cpp b/clang/tools/libclang/CXCursor.cpp index 0fa201a146c..3edbc4d1ef9 100644 --- a/clang/tools/libclang/CXCursor.cpp +++ b/clang/tools/libclang/CXCursor.cpp @@ -16,6 +16,7 @@ #include "CXCursor.h" #include "clang/Frontend/ASTUnit.h" #include "clang/AST/Decl.h" +#include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/Expr.h" #include "llvm/Support/ErrorHandling.h" @@ -60,6 +61,7 @@ static CXCursorKind GetCursorKind(Decl *D) { case Decl::Typedef: return CXCursor_TypedefDecl; case Decl::Var: return CXCursor_VarDecl; case Decl::Namespace: return CXCursor_Namespace; + case Decl::NamespaceAlias: return CXCursor_NamespaceAlias; case Decl::TemplateTypeParm: return CXCursor_TemplateTypeParameter; case Decl::NonTypeTemplateParm:return CXCursor_NonTypeTemplateParameter; case Decl::TemplateTemplateParm:return CXCursor_TemplateTemplateParameter; @@ -71,10 +73,10 @@ static CXCursorKind GetCursorKind(Decl *D) { default: if (TagDecl *TD = dyn_cast<TagDecl>(D)) { switch (TD->getTagKind()) { - case TTK_Struct: return CXCursor_StructDecl; - case TTK_Class: return CXCursor_ClassDecl; - case TTK_Union: return CXCursor_UnionDecl; - case TTK_Enum: return CXCursor_EnumDecl; + case TTK_Struct: return CXCursor_StructDecl; + case TTK_Class: return CXCursor_ClassDecl; + case TTK_Union: return CXCursor_UnionDecl; + case TTK_Enum: return CXCursor_EnumDecl; } } @@ -329,6 +331,24 @@ cxcursor::getCursorTemplateRef(CXCursor C) { reinterpret_cast<uintptr_t>(C.data[1]))); } +CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, + ASTUnit *TU) { + + assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU && + "Invalid arguments!"); + void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); + CXCursor C = { CXCursor_NamespaceRef, { NS, RawLoc, TU } }; + return C; +} + +std::pair<NamedDecl *, SourceLocation> +cxcursor::getCursorNamespaceRef(CXCursor C) { + assert(C.kind == CXCursor_NamespaceRef); + return std::make_pair(static_cast<NamedDecl *>(C.data[0]), + SourceLocation::getFromRawEncoding( + reinterpret_cast<uintptr_t>(C.data[1]))); +} + CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU){ CXCursor C = { CXCursor_CXXBaseSpecifier, { B, 0, TU } }; return C; diff --git a/clang/tools/libclang/CXCursor.h b/clang/tools/libclang/CXCursor.h index 2e5ff5811c0..a5f111edc1d 100644 --- a/clang/tools/libclang/CXCursor.h +++ b/clang/tools/libclang/CXCursor.h @@ -84,7 +84,15 @@ CXCursor MakeCursorTemplateRef(TemplateDecl *Template, SourceLocation Loc, /// \brief Unpack a TemplateRef cursor into the template it references and /// the location where the reference occurred. std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C); - + +/// \brief Create a reference to a namespace or namespace alias at the given +/// location. +CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, ASTUnit *TU); + +/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias +/// it references and the location where the reference occurred. +std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C); + /// \brief Create a CXX base specifier cursor. CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, ASTUnit *TU); |

