From: Argyrios Kyrtzidis Date: Thu, 25 Aug 2011 22:24:47 +0000 (+0000) Subject: [libclang] Fix getting a cursor that points inside tag definition that is part X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6f155de99c59af890817146ec8526bafb6560f1f;p=clang [libclang] Fix getting a cursor that points inside tag definition that is part of a type specifier. e.g. for: typedef struct _MyS { int foo; } MyS; pointing at field 'foo' would give a cursor for the typedef declaration 'MyS' instead of the field. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138593 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/TypeLoc.h b/include/clang/AST/TypeLoc.h index 7b284bad5f..8e3e8d37c8 100644 --- a/include/clang/AST/TypeLoc.h +++ b/include/clang/AST/TypeLoc.h @@ -565,6 +565,12 @@ class TagTypeLoc : public InheritingConcreteTypeLoc { public: TagDecl *getDecl() const { return getTypePtr()->getDecl(); } + + /// \brief True if the tag was defined in this type specifier. + bool isDefinition() const { + return getDecl()->isDefinition() && + (getNameLoc().isInvalid() || getNameLoc() == getDecl()->getLocation()); + } }; /// \brief Wrapper for source info for record types. diff --git a/test/Index/get-cursor.c b/test/Index/get-cursor.c new file mode 100644 index 0000000000..23a4b5cb53 --- /dev/null +++ b/test/Index/get-cursor.c @@ -0,0 +1,14 @@ +struct _MyS { + int foo; +} MyS; + +struct _MyS ww; + +// RUN: c-index-test -cursor-at=%s:1:9 \ +// RUN: -cursor-at=%s:2:9 \ +// RUN: -cursor-at=%s:5:9 \ +// RUN: %s | FileCheck %s + +// CHECK: StructDecl=_MyS:1:8 (Definition) +// CHECK: FieldDecl=foo:2:7 (Definition) +// CHECK: TypeRef=struct _MyS:1:8 diff --git a/test/Index/usrs.m b/test/Index/usrs.m index c5d4ce5ae7..6f906d86f4 100644 --- a/test/Index/usrs.m +++ b/test/Index/usrs.m @@ -150,11 +150,10 @@ int test_multi_declaration(void) { // CHECK-source: usrs.m:10:1: EnumDecl=:10:1 (Definition) Extent=[10:1 - 13:2] // CHECK-source: usrs.m:11:3: EnumConstantDecl=FOO:11:3 (Definition) Extent=[11:3 - 11:6] // CHECK-source: usrs.m:12:3: EnumConstantDecl=BAR:12:3 (Definition) Extent=[12:3 - 12:6] +// CHECK-source: usrs.m:18:3: TypedefDecl=MyStruct:18:3 (Definition) Extent=[15:1 - 18:11] // CHECK-source: usrs.m:15:9: StructDecl=:15:9 (Definition) Extent=[15:9 - 18:2] // CHECK-source: usrs.m:16:7: FieldDecl=wa:16:7 (Definition) Extent=[16:3 - 16:9] // CHECK-source: usrs.m:17:7: FieldDecl=moo:17:7 (Definition) Extent=[17:3 - 17:10] -// CHECK-source: usrs.m:18:3: TypedefDecl=MyStruct:18:3 (Definition) Extent=[15:1 - 18:11] -// CHECK-source: usrs.m:15:9: TypeRef=MyStruct:15:9 Extent=[15:9 - 15:15] // CHECK-source: usrs.m:20:6: EnumDecl=Pizza:20:6 (Definition) Extent=[20:1 - 23:2] // CHECK-source: usrs.m:21:3: EnumConstantDecl=CHEESE:21:3 (Definition) Extent=[21:3 - 21:9] // CHECK-source: usrs.m:22:3: EnumConstantDecl=MUSHROOMS:22:3 (Definition) Extent=[22:3 - 22:12] diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 2ab02795ec..f1f2cb10ad 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -1445,6 +1445,9 @@ bool CursorVisitor::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) { } bool CursorVisitor::VisitTagTypeLoc(TagTypeLoc TL) { + if (TL.isDefinition()) + return Visit(MakeCXCursor(TL.getDecl(), TU)); + return Visit(MakeCursorTypeRef(TL.getDecl(), TL.getNameLoc(), TU)); }