diff options
author | Douglas Gregor <dgregor@apple.com> | 2010-09-01 19:52:22 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2010-09-01 19:52:22 +0000 |
commit | a9aa29cf0b171e0046111e99c555250e45b39a3b (patch) | |
tree | 310c5d35d7163d0c545d037dbfecfb5034209a0f | |
parent | 61a312413c82aa23ef63467cde87f6de1ce8724f (diff) | |
download | bcm5719-llvm-a9aa29cf0b171e0046111e99c555250e45b39a3b.tar.gz bcm5719-llvm-a9aa29cf0b171e0046111e99c555250e45b39a3b.zip |
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.
llvm-svn: 112730
-rw-r--r-- | clang/include/clang-c/Index.h | 4 | ||||
-rw-r--r-- | clang/include/clang/AST/DeclCXX.h | 29 | ||||
-rw-r--r-- | clang/lib/Serialization/ASTReaderDecl.cpp | 8 | ||||
-rw-r--r-- | clang/test/Index/index-templates.cpp | 19 | ||||
-rw-r--r-- | clang/test/Index/load-namespaces.cpp | 10 | ||||
-rw-r--r-- | clang/test/Index/usrs.cpp | 6 | ||||
-rw-r--r-- | clang/tools/libclang/CIndex.cpp | 31 | ||||
-rw-r--r-- | clang/tools/libclang/CIndexUSRs.cpp | 9 | ||||
-rw-r--r-- | clang/tools/libclang/CXCursor.cpp | 5 |
9 files changed, 99 insertions, 22 deletions
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h index 0716f82eaf6..d5e3373b91d 100644 --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -998,9 +998,11 @@ enum CXCursorKind { 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/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h index f5cbfd0d597..a9802bfcaf4 100644 --- a/clang/include/clang/AST/DeclCXX.h +++ b/clang/include/clang/AST/DeclCXX.h @@ -2060,6 +2060,10 @@ public: 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 @@ public: 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 @@ class UnresolvedUsingTypenameDecl : public TypeDecl { 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/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index cb6f29a0770..7adbe1220ff 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -695,10 +695,10 @@ void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { 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/clang/test/Index/index-templates.cpp b/clang/test/Index/index-templates.cpp index 9b860180091..09512630f1e 100644 --- a/clang/test/Index/index-templates.cpp +++ b/clang/test/Index/index-templates.cpp @@ -24,6 +24,14 @@ class vector<Z2> { 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 @@ class vector<Z2> { // 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 @@ class vector<Z2> { // 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/clang/test/Index/load-namespaces.cpp b/clang/test/Index/load-namespaces.cpp index 9c8db0a3a52..adb6183d171 100644 --- a/clang/test/Index/load-namespaces.cpp +++ b/clang/test/Index/load-namespaces.cpp @@ -15,6 +15,12 @@ namespace std0x = std98; 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 @@ using namespace std0x; // 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/clang/test/Index/usrs.cpp b/clang/test/Index/usrs.cpp index 50d209feba1..698aded8438 100644 --- a/clang/test/Index/usrs.cpp +++ b/clang/test/Index/usrs.cpp @@ -61,6 +61,10 @@ using namespace foo; 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 @@ namespace foo_alias2 = foo; // 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/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp index 3c4211efdc5..947b3b28262 100644 --- a/clang/tools/libclang/CIndex.cpp +++ b/clang/tools/libclang/CIndex.cpp @@ -319,6 +319,9 @@ public: 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::VisitNamespaceDecl(NamespaceDecl *D) { } 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 @@ CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { return createCXString("NamespaceAlias"); case CXCursor_UsingDirective: return createCXString("UsingDirective"); + case CXCursor_UsingDeclaration: + return createCXString("UsingDeclaration"); } llvm_unreachable("Unhandled CXCursorKind"); diff --git a/clang/tools/libclang/CIndexUSRs.cpp b/clang/tools/libclang/CIndexUSRs.cpp index a60d1d3e189..8f3dacfad21 100644 --- a/clang/tools/libclang/CIndexUSRs.cpp +++ b/clang/tools/libclang/CIndexUSRs.cpp @@ -86,6 +86,15 @@ public: 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/clang/tools/libclang/CXCursor.cpp b/clang/tools/libclang/CXCursor.cpp index 75806ffa8d7..506033e7663 100644 --- a/clang/tools/libclang/CXCursor.cpp +++ b/clang/tools/libclang/CXCursor.cpp @@ -71,6 +71,11 @@ static CXCursorKind GetCursorKind(Decl *D) { 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()) { |