[cfe-commits] r112676 - in /cfe/trunk: include/clang-c/Index.h test/Index/load-namespaces.cpp test/Index/usrs.cpp tools/libclang/CIndex.cpp tools/libclang/CIndexUSRs.cpp tools/libclang/CXCursor.cpp tools/libclang/CXCursor.h
Douglas Gregor
dgregor at apple.com
Tue Aug 31 16:48:11 PDT 2010
Author: dgregor
Date: Tue Aug 31 18:48:11 2010
New Revision: 112676
URL: http://llvm.org/viewvc/llvm-project?rev=112676&view=rev
Log:
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.
Modified:
cfe/trunk/include/clang-c/Index.h
cfe/trunk/test/Index/load-namespaces.cpp
cfe/trunk/test/Index/usrs.cpp
cfe/trunk/tools/libclang/CIndex.cpp
cfe/trunk/tools/libclang/CIndexUSRs.cpp
cfe/trunk/tools/libclang/CXCursor.cpp
cfe/trunk/tools/libclang/CXCursor.h
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Aug 31 18:48:11 2010
@@ -994,9 +994,10 @@
CXCursor_ClassTemplate = 31,
/** \brief A C++ class template partial specialization. */
CXCursor_ClassTemplatePartialSpecialization = 32,
-
+ /** \brief A C++ namespace alias declaration. */
+ CXCursor_NamespaceAlias = 33,
CXCursor_FirstDecl = CXCursor_UnexposedDecl,
- CXCursor_LastDecl = CXCursor_ClassTemplatePartialSpecialization,
+ CXCursor_LastDecl = CXCursor_NamespaceAlias,
/* References */
CXCursor_FirstRef = 40, /* Decl references */
@@ -1025,7 +1026,11 @@
* template parameter.
*/
CXCursor_TemplateRef = 45,
- CXCursor_LastRef = CXCursor_TemplateRef,
+ /**
+ * \brief A reference to a namespace or namespace alias.
+ */
+ CXCursor_NamespaceRef = 46,
+ CXCursor_LastRef = CXCursor_NamespaceRef,
/* Error conditions */
CXCursor_FirstInvalid = 70,
Modified: cfe/trunk/test/Index/load-namespaces.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/load-namespaces.cpp?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/test/Index/load-namespaces.cpp (original)
+++ cfe/trunk/test/Index/load-namespaces.cpp Tue Aug 31 18:48:11 2010
@@ -10,7 +10,10 @@
void g();
}
-// FIXME: using directives, namespace aliases
+namespace std98 = std;
+namespace std0x = std98;
+
+// FIXME: using directives
// RUN: c-index-test -test-load-source all %s | FileCheck %s
// CHECK: load-namespaces.cpp:3:11: Namespace=std:3:11 (Definition) Extent=[3:11 - 7:2]
@@ -18,5 +21,8 @@
// CHECK: load-namespaces.cpp:5:10: FunctionDecl=f:5:10 Extent=[5:10 - 5:13]
// CHECK: load-namespaces.cpp:9:11: Namespace=std:9:11 (Definition) Extent=[9:11 - 11:2]
// CHECK: load-namespaces.cpp:10:8: FunctionDecl=g:10:8 Extent=[10:8 - 10:11]
-
+// CHECK: load-namespaces.cpp:13:1: NamespaceAlias=std98:13:1 Extent=[13:1 - 13:10]
+// CHECK: load-namespaces.cpp:13:19: NamespaceRef=std:3:11 Extent=[13:19 - 13:22]
+// CHECK: load-namespaces.cpp:14:1: NamespaceAlias=std0x:14:1 Extent=[14:1 - 14:10]
+// CHECK: load-namespaces.cpp:14:19: NamespaceRef=std98:13:1 Extent=[14:19 - 14:24]
Modified: cfe/trunk/test/Index/usrs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/usrs.cpp?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/test/Index/usrs.cpp (original)
+++ cfe/trunk/test/Index/usrs.cpp Tue Aug 31 18:48:11 2010
@@ -55,6 +55,8 @@
void rez(int a, int b);
}
+namespace foo_alias = foo;
+
// RUN: c-index-test -test-load-source-usrs all %s | FileCheck %s
// CHECK: usrs.cpp c:@N at foo Extent=[1:11 - 4:2]
// CHECK: usrs.cpp c:@N at foo@x Extent=[2:3 - 2:8]
@@ -107,4 +109,4 @@
// CHECK: usrs.cpp c:@F at rez Extent=[55:8 - 55:25]
// CHECK: usrs.cpp c:usrs.cpp at 941@F at rez@a Extent=[55:12 - 55:17]
// CHECK: usrs.cpp c:usrs.cpp at 948@F at rez@b Extent=[55:19 - 55:24]
-
+// CHECK: usrs.cpp c:@NA at foo_alias
Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Aug 31 18:48:11 2010
@@ -316,6 +316,7 @@
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 @@
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 @@
}
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 @@
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 @@
return createCXString("ClassTemplate");
case CXCursor_ClassTemplatePartialSpecialization:
return createCXString("ClassTemplatePartialSpecialization");
+ case CXCursor_NamespaceAlias:
+ return createCXString("NamespaceAlias");
}
llvm_unreachable("Unhandled CXCursorKind");
@@ -2329,6 +2348,11 @@
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 @@
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 @@
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(),
Modified: cfe/trunk/tools/libclang/CIndexUSRs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexUSRs.cpp?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexUSRs.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexUSRs.cpp Tue Aug 31 18:48:11 2010
@@ -65,6 +65,7 @@
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 @@
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());
Modified: cfe/trunk/tools/libclang/CXCursor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCursor.cpp?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCursor.cpp (original)
+++ cfe/trunk/tools/libclang/CXCursor.cpp Tue Aug 31 18:48:11 2010
@@ -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 @@
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 @@
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 @@
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;
Modified: cfe/trunk/tools/libclang/CXCursor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXCursor.h?rev=112676&r1=112675&r2=112676&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXCursor.h (original)
+++ cfe/trunk/tools/libclang/CXCursor.h Tue Aug 31 18:48:11 2010
@@ -84,7 +84,15 @@
/// \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);
More information about the cfe-commits
mailing list