Implement libclang support for using declarations. Clang actually uses
three different kinds of AST nodes to represent using declarations:
UsingDecl, UnresolvedUsingValueDecl, and
UnresolvedUsingTypenameDecl. These three are collapsed into a single
cursor kind for using declarations, since libclang clients don't need
the distinction.
Several related changes here:
- Cursor visitation of the three AST nodes for using declarations
- Proper source-range computation for these AST nodes
- Using declarations have no USRs, since they don't actually declare
any entities.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112730 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index 0716f82..d5e3373 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -998,9 +998,11 @@
CXCursor_NamespaceAlias = 33,
/** \brief A C++ using directive. */
CXCursor_UsingDirective = 34,
+ /** \brief A using declaration. */
+ CXCursor_UsingDeclaration = 35,
CXCursor_FirstDecl = CXCursor_UnexposedDecl,
- CXCursor_LastDecl = CXCursor_UsingDirective,
+ CXCursor_LastDecl = CXCursor_UsingDeclaration,
/* References */
CXCursor_FirstRef = 40, /* Decl references */
diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h
index f5cbfd0..a9802bf 100644
--- a/include/clang/AST/DeclCXX.h
+++ b/include/clang/AST/DeclCXX.h
@@ -2060,6 +2060,10 @@
const DeclarationNameInfo &NameInfo,
bool IsTypeNameArg);
+ SourceRange getSourceRange() const {
+ return SourceRange(UsingLocation, getNameInfo().getEndLoc());
+ }
+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classof(const UsingDecl *D) { return true; }
static bool classofKind(Kind K) { return K == Using; }
@@ -2134,6 +2138,10 @@
SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
const DeclarationNameInfo &NameInfo);
+ SourceRange getSourceRange() const {
+ return SourceRange(UsingLocation, getNameInfo().getEndLoc());
+ }
+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classof(const UnresolvedUsingValueDecl *D) { return true; }
static bool classofKind(Kind K) { return K == UnresolvedUsingValue; }
@@ -2170,43 +2178,34 @@
TypenameLocation(TypenameLoc), TargetNestedNameSpecifier(TargetNNS)
{ }
+ friend class ASTDeclReader;
+
public:
/// \brief Returns the source range that covers the nested-name-specifier
/// preceding the namespace name.
SourceRange getTargetNestedNameRange() const { return TargetNestedNameRange; }
- /// \brief Set the source range coverting the nested-name-specifier preceding
- /// the namespace name.
- void setTargetNestedNameRange(SourceRange R) { TargetNestedNameRange = R; }
-
/// \brief Get target nested name declaration.
NestedNameSpecifier* getTargetNestedNameSpecifier() {
return TargetNestedNameSpecifier;
}
- /// \brief Set the nested name declaration.
- void setTargetNestedNameSpecifier(NestedNameSpecifier* NNS) {
- TargetNestedNameSpecifier = NNS;
- }
-
/// \brief Returns the source location of the 'using' keyword.
SourceLocation getUsingLoc() const { return UsingLocation; }
- /// \brief Set the source location of the 'using' keyword.
- void setUsingLoc(SourceLocation L) { UsingLocation = L; }
-
/// \brief Returns the source location of the 'typename' keyword.
SourceLocation getTypenameLoc() const { return TypenameLocation; }
- /// \brief Set the source location of the 'typename' keyword.
- void setTypenameLoc(SourceLocation L) { TypenameLocation = L; }
-
static UnresolvedUsingTypenameDecl *
Create(ASTContext &C, DeclContext *DC, SourceLocation UsingLoc,
SourceLocation TypenameLoc,
SourceRange TargetNNR, NestedNameSpecifier *TargetNNS,
SourceLocation TargetNameLoc, DeclarationName TargetName);
+ SourceRange getSourceRange() const {
+ return SourceRange(UsingLocation, getLocation());
+ }
+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classof(const UnresolvedUsingTypenameDecl *D) { return true; }
static bool classofKind(Kind K) { return K == UnresolvedUsingTypename; }
diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp
index cb6f29a..7adbe12 100644
--- a/lib/Serialization/ASTReaderDecl.cpp
+++ b/lib/Serialization/ASTReaderDecl.cpp
@@ -695,10 +695,10 @@
void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
UnresolvedUsingTypenameDecl *D) {
VisitTypeDecl(D);
- D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
- D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
- D->setTypenameLoc(Reader.ReadSourceLocation(Record, Idx));
- D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
+ D->TargetNestedNameRange = Reader.ReadSourceRange(Record, Idx);
+ D->UsingLocation = Reader.ReadSourceLocation(Record, Idx);
+ D->TypenameLocation = Reader.ReadSourceLocation(Record, Idx);
+ D->TargetNestedNameSpecifier = Reader.ReadNestedNameSpecifier(Record, Idx);
}
void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
diff --git a/test/Index/index-templates.cpp b/test/Index/index-templates.cpp
index 9b86018..0951263 100644
--- a/test/Index/index-templates.cpp
+++ b/test/Index/index-templates.cpp
@@ -24,6 +24,14 @@
void clear();
};
+template<typename T, typename U>
+struct Y {
+ using typename T::type;
+ using U::operator Z2;
+};
+
+struct Z3 { };
+
// RUN: c-index-test -test-load-source all %s | FileCheck -check-prefix=CHECK-LOAD %s
// CHECK-LOAD: index-templates.cpp:4:6: FunctionTemplate=f:4:6 Extent=[3:1 - 4:22]
// CHECK-LOAD: index-templates.cpp:3:19: TemplateTypeParameter=T:3:19 (Definition) Extent=[3:19 - 3:20]
@@ -50,6 +58,12 @@
// CHECK-LOAD: index-templates.cpp:23:7: ClassDecl=vector:23:7 (Definition) Extent=[22:1 - 25:2]
// CHECK-LOAD: index-templates.cpp:23:14: TypeRef=struct Z2:20:8 Extent=[23:14 - 23:16]
// CHECK-LOAD: index-templates.cpp:24:8: CXXMethod=clear:24:8 Extent=[24:8 - 24:15]
+// CHECK-LOAD: index-templates.cpp:28:8: ClassTemplate=Y:28:8 (Definition) Extent=[27:1 - 31:2]
+// CHECK-LOAD: index-templates.cpp:27:19: TemplateTypeParameter=T:27:19 (Definition) Extent=[27:19 - 27:20]
+// CHECK-LOAD: index-templates.cpp:27:31: TemplateTypeParameter=U:27:31 (Definition) Extent=[27:31 - 27:32]
+// CHECK-LOAD: index-templates.cpp:29:21: UsingDeclaration=type:29:21 Extent=[29:3 - 29:25]
+// CHECK-LOAD: index-templates.cpp:30:12: UsingDeclaration=operator Z2:30:12 Extent=[30:3 - 30:23]
+// CHECK-LOAD: index-templates.cpp:30:21: TypeRef=struct Z2:20:8 Extent=[30:21 - 30:23]
// RUN: c-index-test -test-load-source-usrs all %s | FileCheck -check-prefix=CHECK-USRS %s
// CHECK-USRS: index-templates.cpp c:@FT@>3#T#Nt0.0#t>2#T#Nt1.0f#>t0.22t0.0# Extent=[3:1 - 4:22]
@@ -70,3 +84,8 @@
// CHECK-USRS: index-templates.cpp c:@S@Z2 Extent=[20:1 - 20:14]
// CHECK-USRS: index-templates.cpp c:@C@vector>#$@S@Z2#$@C@allocator>#$@S@Z2 Extent=[22:1 - 25:2]
// CHECK-USRS: index-templates.cpp c:@C@vector>#$@S@Z2#$@C@allocator>#$@S@Z2@F@clear# Extent=[24:8 - 24:15]
+// CHECK-USRS: index-templates.cpp c:@ST>2#T#T@Y Extent=[27:1 - 31:2]
+// CHECK-USRS: index-templates.cpp c:index-templates.cpp@452 Extent=[27:19 - 27:20]
+// CHECK-USRS: index-templates.cpp c:index-templates.cpp@464 Extent=[27:31 - 27:32]
+// CHECK-USRS-NOT: type
+// CHECK-USRS: index-templates.cpp c:@S@Z3 Extent=[33:1 - 33:14]
diff --git a/test/Index/load-namespaces.cpp b/test/Index/load-namespaces.cpp
index 9c8db0a..adb6183 100644
--- a/test/Index/load-namespaces.cpp
+++ b/test/Index/load-namespaces.cpp
@@ -15,6 +15,12 @@
using namespace std0x;
+namespace std {
+ int g(int);
+}
+
+using std::g;
+
// 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]
@@ -27,3 +33,7 @@
// CHECK: load-namespaces.cpp:14:19: NamespaceRef=std98:13:11 Extent=[14:19 - 14:24]
// CHECK: load-namespaces.cpp:16:17: UsingDirective=:16:17 Extent=[16:1 - 16:22]
// CHECK: load-namespaces.cpp:16:17: NamespaceRef=std0x:14:11 Extent=[16:17 - 16:22]
+// CHECK: load-namespaces.cpp:18:11: Namespace=std:18:11 (Definition) Extent=[18:11 - 20:2]
+// 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]
diff --git a/test/Index/usrs.cpp b/test/Index/usrs.cpp
index 50d209f..698aded 100644
--- a/test/Index/usrs.cpp
+++ b/test/Index/usrs.cpp
@@ -61,6 +61,10 @@
namespace foo_alias2 = foo;
+using foo::ClsB;
+
+namespace foo_alias3 = foo;
+
// RUN: c-index-test -test-load-source-usrs all %s | FileCheck %s
// CHECK: usrs.cpp c:@N@foo Extent=[1:11 - 4:2]
// CHECK: usrs.cpp c:@N@foo@x Extent=[2:3 - 2:8]
@@ -116,3 +120,5 @@
// CHECK: usrs.cpp c:@NA@foo_alias
// CHECK-NOT: foo
// CHECK: usrs.cpp c:@NA@foo_alias2
+// CHECK-NOT: ClsB
+// CHECK: usrs.cpp c:@NA@foo_alias3
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 3c4211e..947b3b2 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -319,6 +319,9 @@
bool VisitNamespaceDecl(NamespaceDecl *D);
bool VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
+ bool VisitUsingDecl(UsingDecl *D);
+ bool VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
+ bool VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
// Name visitor
bool VisitDeclarationNameInfo(DeclarationNameInfo Name);
@@ -902,19 +905,41 @@
}
bool CursorVisitor::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
- // FIXME: Visit nested-name-specifier
+ // FIXME: Visit nested-name-specifier.
return Visit(MakeCursorNamespaceRef(D->getAliasedNamespace(),
D->getTargetNameLoc(), TU));
}
+bool CursorVisitor::VisitUsingDecl(UsingDecl *D) {
+ // FIXME: Visit nested-name-specifier.
+
+ // 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
+ // yet.
+
+ return VisitDeclarationNameInfo(D->getNameInfo());
+}
+
bool CursorVisitor::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
- // FIXME: Visit nested-name-specifier
+ // FIXME: Visit nested-name-specifier.
return Visit(MakeCursorNamespaceRef(D->getNominatedNamespaceAsWritten(),
D->getIdentLocation(), TU));
}
+bool CursorVisitor::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
+ // FIXME: Visit nested-name-specifier.
+
+ return VisitDeclarationNameInfo(D->getNameInfo());
+}
+
+bool CursorVisitor::VisitUnresolvedUsingTypenameDecl(
+ UnresolvedUsingTypenameDecl *D) {
+ // FIXME: Visit nested-name-specifier.
+ return false;
+}
+
bool CursorVisitor::VisitDeclarationNameInfo(DeclarationNameInfo Name) {
switch (Name.getName().getNameKind()) {
case clang::DeclarationName::Identifier:
@@ -2268,6 +2293,8 @@
return createCXString("NamespaceAlias");
case CXCursor_UsingDirective:
return createCXString("UsingDirective");
+ case CXCursor_UsingDeclaration:
+ return createCXString("UsingDeclaration");
}
llvm_unreachable("Unhandled CXCursorKind");
diff --git a/tools/libclang/CIndexUSRs.cpp b/tools/libclang/CIndexUSRs.cpp
index a60d1d3..8f3dacf 100644
--- a/tools/libclang/CIndexUSRs.cpp
+++ b/tools/libclang/CIndexUSRs.cpp
@@ -86,6 +86,15 @@
void VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
IgnoreResults = true;
}
+ void VisitUsingDecl(UsingDecl *D) {
+ IgnoreResults = true;
+ }
+ void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
+ IgnoreResults = true;
+ }
+ void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) {
+ IgnoreResults = true;
+ }
/// Generate the string component containing the location of the
/// declaration.
diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp
index 75806ff..506033e 100644
--- a/tools/libclang/CXCursor.cpp
+++ b/tools/libclang/CXCursor.cpp
@@ -71,6 +71,11 @@
return CXCursor_ClassTemplatePartialSpecialization;
case Decl::UsingDirective: return CXCursor_UsingDirective;
+ case Decl::Using:
+ case Decl::UnresolvedUsingValue:
+ case Decl::UnresolvedUsingTypename:
+ return CXCursor_UsingDeclaration;
+
default:
if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
switch (TD->getTagKind()) {