From c5ade2e3644a5822df63e442788d68c591ccdc97 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 2 Sep 2010 17:35:32 +0000 Subject: [PATCH] Implement basic visitation for nested name specifiers via libclang cursors. Sadly, this visitation is a hack, because we don't have proper source-location information for nested-name-specifiers in the AST. It does improve on the status quo, however. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112837 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/Index/load-classes.cpp | 6 +++ test/Index/load-namespaces.cpp | 11 +++++ tools/libclang/CIndex.cpp | 78 +++++++++++++++++++++++++++++++--- 3 files changed, 88 insertions(+), 7 deletions(-) diff --git a/test/Index/load-classes.cpp b/test/Index/load-classes.cpp index 478bf61a44..349ebdecd7 100644 --- a/test/Index/load-classes.cpp +++ b/test/Index/load-classes.cpp @@ -7,6 +7,9 @@ struct X { operator X*(); }; +X::X(int value) { +} + // RUN: c-index-test -test-load-source all %s | FileCheck %s // CHECK: load-classes.cpp:3:8: StructDecl=X:3:8 (Definition) Extent=[3:1 - 8:2] // CHECK: load-classes.cpp:4:3: CXXConstructor=X:4:3 Extent=[4:3 - 4:15] @@ -20,3 +23,6 @@ struct X { // FIXME: missing TypeRef in the destructor name // CHECK: load-classes.cpp:7:3: CXXConversion=operator struct X *:7:3 Extent=[7:3 - 7:16] // CHECK: load-classes.cpp:7:12: TypeRef=struct X:3:8 Extent=[7:12 - 7:13] +// CHECK: load-classes.cpp:10:4: CXXConstructor=X:10:4 (Definition) Extent=[10:4 - 11:2] +// CHECK: load-classes.cpp:10:1: TypeRef=struct X:3:8 Extent=[10:1 - 10:2] +// CHECK: load-classes.cpp:10:10: ParmDecl=value:10:10 (Definition) Extent=[10:6 - 10:15] diff --git a/test/Index/load-namespaces.cpp b/test/Index/load-namespaces.cpp index adb6183d17..241e2413a7 100644 --- a/test/Index/load-namespaces.cpp +++ b/test/Index/load-namespaces.cpp @@ -21,6 +21,11 @@ namespace std { using std::g; +void std::g() { +} + +namespace my_rel_ops = std::rel_ops; + // 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] // CHECK: load-namespaces.cpp:4:13: Namespace=rel_ops:4:13 (Definition) Extent=[4:13 - 6:4] @@ -37,3 +42,9 @@ using std::g; // CHECK: load-namespaces.cpp:19:7: FunctionDecl=g:19:7 Extent=[19:7 - 19:13] // CHECK: load-namespaces.cpp:19:12: ParmDecl=:19:12 (Definition) Extent=[19:9 - 19:13] // CHECK: load-namespaces.cpp:22:12: UsingDeclaration=g:22:12 Extent=[22:1 - 22:13] +// CHECK: load-namespaces.cpp:22:7: NamespaceRef=std:18:11 Extent=[22:7 - 22:10] +// CHECK: load-namespaces.cpp:24:11: FunctionDecl=g:24:11 (Definition) Extent=[24:11 - 25:2] +// CHECK: load-namespaces.cpp:24:6: NamespaceRef=std:18:11 Extent=[24:6 - 24:9] +// CHECK: load-namespaces.cpp:27:11: NamespaceAlias=my_rel_ops:27:11 Extent=[27:1 - 27:36] +// CHECK: load-namespaces.cpp:27:24: NamespaceRef=std:18:11 Extent=[27:24 - 27:27] +// CHECK: load-namespaces.cpp:27:29: NamespaceRef=rel_ops:4:13 Extent=[27:29 - 27:36] diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 966562def5..89aa92cf7d 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -327,6 +327,7 @@ public: // Name visitor bool VisitDeclarationNameInfo(DeclarationNameInfo Name); + bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS, SourceRange Range); // Template visitors bool VisitTemplateParameters(const TemplateParameterList *Params); @@ -700,7 +701,10 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) { (!FTL && Visit(TL))) return true; - // FIXME: Visit the nested-name-specifier, if present. + // Visit the nested-name-specifier, if present. + if (NestedNameSpecifier *Qualifier = ND->getQualifier()) + if (VisitNestedNameSpecifier(Qualifier, ND->getQualifierRange())) + return true; // Visit the declaration name. if (VisitDeclarationNameInfo(ND->getNameInfo())) @@ -934,14 +938,20 @@ bool CursorVisitor::VisitNamespaceDecl(NamespaceDecl *D) { } bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { - // FIXME: Visit nested-name-specifier. + // Visit nested-name-specifier. + if (NestedNameSpecifier *Qualifier = D->getQualifier()) + if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange())) + return true; return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(), D->getTargetNameLoc(), TU)); } bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { - // FIXME: Visit nested-name-specifier. + // Visit nested-name-specifier. + if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameDecl()) + if (VisitNestedNameSpecifier(Qualifier, D->getNestedNameRange())) + return true; // FIXME: Provide a multi-reference of some kind for all of the declarations // that the using declaration refers to. We don't have this kind of cursor @@ -951,21 +961,31 @@ bool CursorVisitor::VisitUsingDecl(UsingDecl *D) { } bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { - // FIXME: Visit nested-name-specifier. + // Visit nested-name-specifier. + if (NestedNameSpecifier *Qualifier = D->getQualifier()) + if (VisitNestedNameSpecifier(Qualifier, D->getQualifierRange())) + return true; return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(), D->getIdentLocation(), TU)); } bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { - // FIXME: Visit nested-name-specifier. - + // Visit nested-name-specifier. + if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameSpecifier()) + if (VisitNestedNameSpecifier(Qualifier, D->getTargetNestedNameRange())) + return true; + return VisitDeclarationNameInfo(D->getNameInfo()); } bool CursorVisitor::VisitUnresolvedUsingTypenameDecl( UnresolvedUsingTypenameDecl *D) { - // FIXME: Visit nested-name-specifier. + // Visit nested-name-specifier. + if (NestedNameSpecifier *Qualifier = D->getTargetNestedNameSpecifier()) + if (VisitNestedNameSpecifier(Qualifier, D->getTargetNestedNameRange())) + return true; + return false; } @@ -994,6 +1014,50 @@ bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) { return false; } +bool CursorVisitor::VisitNestedNameSpecifier(NestedNameSpecifier *NNS, + SourceRange Range) { + // FIXME: This whole routine is a hack to work around the lack of proper + // source information in nested-name-specifiers (PR5791). Since we do have + // a beginning source location, we can visit the first component of the + // nested-name-specifier, if it's a single-token component. + if (!NNS) + return false; + + // Get the first component in the nested-name-specifier. + while (NestedNameSpecifier *Prefix = NNS->getPrefix()) + NNS = Prefix; + + switch (NNS->getKind()) { + case NestedNameSpecifier::Namespace: + // FIXME: The token at this source location might actually have been a + // namespace alias, but we don't model that. Lame! + return Visit(MakeCursorNamespaceRef(NNS->getAsNamespace(), Range.getBegin(), + TU)); + + case NestedNameSpecifier::TypeSpec: { + // If the type has a form where we know that the beginning of the source + // range matches up with a reference cursor. Visit the appropriate reference + // cursor. + Type *T = NNS->getAsType(); + if (const TypedefType *Typedef = dyn_cast(T)) + return Visit(MakeCursorTypeRef(Typedef->getDecl(), Range.getBegin(), TU)); + if (const TagType *Tag = dyn_cast(T)) + return Visit(MakeCursorTypeRef(Tag->getDecl(), Range.getBegin(), TU)); + if (const TemplateSpecializationType *TST + = dyn_cast(T)) + return VisitTemplateName(TST->getTemplateName(), Range.getBegin()); + break; + } + + case NestedNameSpecifier::TypeSpecWithTemplate: + case NestedNameSpecifier::Global: + case NestedNameSpecifier::Identifier: + break; + } + + return false; +} + bool CursorVisitor::VisitTemplateParameters( const TemplateParameterList *Params) { if (!Params)