r359448 - [libclang] Restore old clang_Cursor_isAnonymous behaviour

Ivan Donchevskii via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 29 06:44:07 PDT 2019


Author: yvvan
Date: Mon Apr 29 06:44:07 2019
New Revision: 359448

URL: http://llvm.org/viewvc/llvm-project?rev=359448&view=rev
Log:
[libclang] Restore old clang_Cursor_isAnonymous behaviour

D54996 Changed the behaviour of clang_Cursor_isAnonymous, but there is no alternative available to get the old behaviour in some cases, which is essential for determining if a record is syntactically accessible, e.g.

struct {
  int x;
  int y;
} foo;

struct {
  struct {
    int x;
    int y;
  };
} bar;

void fun(struct { int x; int y; } *param);
The only 'anonymous' struct here is the one nested in bar, since there is
no way to reference the struct itself, only the fields within. Though the
anonymity applies to the instance itself, not the type.

To avoid confusion, I have added a new function called clang_Cursor_isAnonymousRecordDecl
which has the old behaviour of clang_Cursor_isAnonymous (and updated the doc
for the latter as well, which was seemingly forgotten).

Patch by Jorn Vernee.

Differential Revision: https://reviews.llvm.org/D61232

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/test/Index/print-type.c
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CXType.cpp

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=359448&r1=359447&r2=359448&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Mon Apr 29 06:44:07 2019
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 55
+#define CINDEX_VERSION_MINOR 56
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -3921,10 +3921,16 @@ CINDEX_LINKAGE CXType clang_Type_getModi
 CINDEX_LINKAGE long long clang_Cursor_getOffsetOfField(CXCursor C);
 
 /**
+ * Determine whether the given cursor represents an anonymous
+ * tag or namespace
+ */
+CINDEX_LINKAGE unsigned clang_Cursor_isAnonymous(CXCursor C);
+
+/**
  * Determine whether the given cursor represents an anonymous record
  * declaration.
  */
-CINDEX_LINKAGE unsigned clang_Cursor_isAnonymous(CXCursor C);
+CINDEX_LINKAGE unsigned clang_Cursor_isAnonymousRecordDecl(CXCursor C);
 
 enum CXRefQualifierKind {
   /** No ref-qualifier was provided. */

Modified: cfe/trunk/test/Index/print-type.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-type.c?rev=359448&r1=359447&r2=359448&view=diff
==============================================================================
--- cfe/trunk/test/Index/print-type.c (original)
+++ cfe/trunk/test/Index/print-type.c Mon Apr 29 06:44:07 2019
@@ -15,6 +15,20 @@ int f2(int incompletearray[]);
 enum Enum{i}; enum Enum elaboratedEnumType();
 struct Struct{}; struct Struct elaboratedStructType();
 
+struct {
+  int x;
+  int y;
+} foo;
+
+struct {
+  struct {
+    int x;
+    int y;
+  };
+} bar;
+
+void fun(struct { int x; int y; } *param);
+
 // RUN: c-index-test -test-print-type %s | FileCheck %s
 // CHECK: FunctionDecl=f:3:6 (Definition) [type=int *(int *, char *, FooType, int *, void (*)(int))] [typekind=FunctionProto] [canonicaltype=int *(int *, char *, int, int *, void (*)(int))] [canonicaltypekind=FunctionProto] [resulttype=int *] [resulttypekind=Pointer] [args= [int *] [Pointer] [char *] [Pointer] [FooType] [Typedef] [int [5]] [ConstantArray] [void (*)(int)] [Pointer]] [isPOD=0]
 // CHECK: ParmDecl=p:3:13 (Definition) [type=int *] [typekind=Pointer] [isPOD=1] [pointeetype=int] [pointeekind=Int]
@@ -53,3 +67,7 @@ struct Struct{}; struct Struct elaborate
 // CHECK: StructDecl=Struct:16:8 (Definition) [type=struct Struct] [typekind=Record] [isPOD=1]
 // CHECK: FunctionDecl=elaboratedStructType:16:32 [type=struct Struct ()] [typekind=FunctionNoProto] [canonicaltype=struct Struct ()] [canonicaltypekind=FunctionNoProto] [resulttype=struct Struct] [resulttypekind=Elaborated] [isPOD=0]
 // CHECK: TypeRef=struct Struct:16:8 [type=struct Struct] [typekind=Record] [isPOD=1]
+// CHECK: StructDecl=:18:1 (Definition) [type=struct (anonymous at {{.*}}print-type.c:18:1)] [typekind=Record] [isPOD=1] [nbFields=2] [isAnon=1] [isAnonRecDecl=0]
+// CHECK: StructDecl=:23:1 (Definition) [type=struct (anonymous at {{.*}}print-type.c:23:1)] [typekind=Record] [isPOD=1] [nbFields=1] [isAnon=1] [isAnonRecDecl=0]
+// CHECK: StructDecl=:24:3 (Definition) [type=struct (anonymous at {{.*}}print-type.c:24:3)] [typekind=Record] [isPOD=1] [nbFields=2] [isAnon=1] [isAnonRecDecl=1]
+// CHECK: StructDecl=:30:10 (Definition) [type=struct (anonymous at {{.*}}print-type.c:30:10)] [typekind=Record] [isPOD=1] [nbFields=2] [isAnon=1] [isAnonRecDecl=0]

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=359448&r1=359447&r2=359448&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Mon Apr 29 06:44:07 2019
@@ -1665,6 +1665,12 @@ static enum CXChildVisitResult PrintType
       }
     }
 
+    /* Print if it is an anonymous record decl */
+    {
+      unsigned isAnonRecDecl = clang_Cursor_isAnonymousRecordDecl(cursor);
+      printf(" [isAnonRecDecl=%d]", isAnonRecDecl);
+    }
+
     printf("\n");
   }
   return CXChildVisit_Recurse;

Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=359448&r1=359447&r2=359448&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Mon Apr 29 06:44:07 2019
@@ -1253,6 +1253,16 @@ unsigned clang_Cursor_isAnonymous(CXCurs
 
   return 0;
 }
+
+unsigned clang_Cursor_isAnonymousRecordDecl(CXCursor C){
+  if (!clang_isDeclaration(C.kind))
+    return 0;
+  const Decl *D = cxcursor::getCursorDecl(C);
+  if (const RecordDecl *FD = dyn_cast_or_null<RecordDecl>(D))
+    return FD->isAnonymousStructOrUnion();
+  return 0;
+}
+
 CXType clang_Type_getNamedType(CXType CT){
   QualType T = GetQualType(CT);
   const Type *TP = T.getTypePtrOrNull();




More information about the cfe-commits mailing list