]> granicus.if.org Git - clang/commitdiff
[libclang] Fix getting a cursor that points inside tag definition that is part
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 25 Aug 2011 22:24:47 +0000 (22:24 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 25 Aug 2011 22:24:47 +0000 (22:24 +0000)
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

include/clang/AST/TypeLoc.h
test/Index/get-cursor.c [new file with mode: 0644]
test/Index/usrs.m
tools/libclang/CIndex.cpp

index 7b284bad5f236fa2fef16dccc1cf13218a0300a6..8e3e8d37c87ed6fb363700546266307bbdd8efd9 100644 (file)
@@ -565,6 +565,12 @@ class TagTypeLoc : public InheritingConcreteTypeLoc<TypeSpecTypeLoc,
                                                     TagType> {
 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 (file)
index 0000000..23a4b5c
--- /dev/null
@@ -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
index c5d4ce5ae7605acf25614e693466c64e428608f7..6f906d86f4e1bce77e711298a3bfbe9d66cf18a2 100644 (file)
@@ -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]
index 2ab02795ecf7eed829beeff73a7bc2d2e1dd2671..f1f2cb10adf044cd8d560802f736132a384db787 100644 (file)
@@ -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));
 }