r350805 - [libclang] Fix clang_Cursor_isAnonymous

Ivan Donchevskii via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 10 01:34:45 PST 2019


Author: yvvan
Date: Thu Jan 10 01:34:44 2019
New Revision: 350805

URL: http://llvm.org/viewvc/llvm-project?rev=350805&view=rev
Log:
[libclang] Fix clang_Cursor_isAnonymous

Use the same logic as in TypePrinter::printTag to determine that the tag is anonymous and the separate check for namespaces.

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

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

Modified: cfe/trunk/test/Index/print-type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-type.cpp?rev=350805&r1=350804&r2=350805&view=diff
==============================================================================
--- cfe/trunk/test/Index/print-type.cpp (original)
+++ cfe/trunk/test/Index/print-type.cpp Thu Jan 10 01:34:44 2019
@@ -79,6 +79,17 @@ auto autoTemplPointer = &autoTemplRefPar
 
 outer::Foo<bool> parameter;
 outer::inner::Bar construct(&parameter);
+
+class X {
+  struct { int a; };
+  class { public: int b; };
+  union { int c; int d;};
+  enum { Test };
+};
+
+namespace {
+  int a;
+}
 // RUN: c-index-test -test-print-type %s -std=c++14 | FileCheck %s
 // CHECK: Namespace=outer:1:11 (Definition) [type=] [typekind=Invalid] [isPOD=0]
 // CHECK: ClassTemplate=Foo:4:8 (Definition) [type=] [typekind=Invalid] [isPOD=0]
@@ -188,3 +199,8 @@ outer::inner::Bar construct(&parameter);
 // CHECK: TypeAliasDecl=baz:76:7 (Definition) [type=baz] [typekind=Typedef] [templateargs/1= [type=A<void>] [typekind=Unexposed]] [canonicaltype=A<void>] [canonicaltypekind=Record] [canonicaltemplateargs/1= [type=void] [typekind=Void]] [isPOD=0]
 // CHECK: VarDecl=autoTemplPointer:78:6 (Definition) [type=Specialization<Specialization<bool> &> *] [typekind=Auto] [canonicaltype=Specialization<Specialization<bool> &> *] [canonicaltypekind=Pointer] [isPOD=1] [pointeetype=Specialization<Specialization<bool> &>] [pointeekind=Record]
 // CHECK: CallExpr=Bar:17:3 [type=outer::inner::Bar] [typekind=Elaborated] [canonicaltype=outer::inner::Bar] [canonicaltypekind=Record] [args= [outer::Foo<bool> *] [Pointer]] [isPOD=0] [nbFields=3]
+// CHECK: StructDecl=:84:3 (Definition) [type=X::(anonymous struct at {{.*}}print-type.cpp:84:3)] [typekind=Record] [isPOD=1] [nbFields=1] [isAnon=1]
+// CHECK: ClassDecl=:85:3 (Definition) [type=X::(anonymous class at {{.*}}print-type.cpp:85:3)] [typekind=Record] [isPOD=1] [nbFields=1] [isAnon=1]
+// CHECK: UnionDecl=:86:3 (Definition) [type=X::(anonymous union at {{.*}}print-type.cpp:86:3)] [typekind=Record] [isPOD=1] [nbFields=2] [isAnon=1]
+// CHECK: EnumDecl=:87:3 (Definition) [type=X::(anonymous enum at {{.*}}print-type.cpp:87:3)] [typekind=Enum] [isPOD=1] [isAnon=1]
+// CHECK: Namespace=:90:11 (Definition) [type=] [typekind=Invalid] [isPOD=0] [isAnon=1]

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=350805&r1=350804&r2=350805&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Jan 10 01:34:44 2019
@@ -1654,13 +1654,14 @@ static enum CXChildVisitResult PrintType
         if (numFields != 0) {
           printf(" [nbFields=%d]", numFields);
         }
-        /* Print if it is an anonymous record. */
-        {
-          unsigned isAnon = clang_Cursor_isAnonymous(cursor);
-          if (isAnon != 0) {
-            printf(" [isAnon=%d]", isAnon);
-          }
-        }
+      }
+    }
+
+    /* Print if it is an anonymous record or namespace. */
+    {
+      unsigned isAnon = clang_Cursor_isAnonymous(cursor);
+      if (isAnon != 0) {
+        printf(" [isAnon=%d]", isAnon);
       }
     }
 

Modified: cfe/trunk/tools/libclang/CXType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXType.cpp?rev=350805&r1=350804&r2=350805&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXType.cpp (original)
+++ cfe/trunk/tools/libclang/CXType.cpp Thu Jan 10 01:34:44 2019
@@ -1229,11 +1229,15 @@ unsigned clang_Cursor_isAnonymous(CXCurs
   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();
+  if (const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D)) {
+    return ND->isAnonymousNamespace();
+  } else if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(D)) {
+    return TD->getTypedefNameForAnonDecl() == nullptr &&
+           TD->getIdentifier() == nullptr;
+  }
+
   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