r307771 - [libclang] Support for querying whether an enum is scoped

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 12 04:35:11 PDT 2017


Author: arphaman
Date: Wed Jul 12 04:35:11 2017
New Revision: 307771

URL: http://llvm.org/viewvc/llvm-project?rev=307771&view=rev
Log:
[libclang] Support for querying whether an enum is scoped

This commit allows checking whether an enum declaration is scoped
through libclang and clang.cindex (Python).

Patch by Johann Klähn!

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

Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_cursor.py
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/test/Index/print-type-declaration.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Wed Jul 12 04:35:11 2017
@@ -1478,6 +1478,11 @@ class Cursor(Structure):
         """
         return conf.lib.clang_CXXMethod_isVirtual(self)
 
+    def is_scoped_enum(self):
+        """Returns True if the cursor refers to a scoped enum declaration.
+        """
+        return conf.lib.clang_EnumDecl_isScoped(self)
+
     def get_definition(self):
         """
         If the cursor is a reference to a declaration or a declaration of
@@ -3314,6 +3319,10 @@ functionList = [
    [Cursor],
    bool),
 
+  ("clang_EnumDecl_isScoped",
+   [Cursor],
+   bool),
+
   ("clang_defaultDiagnosticDisplayOptions",
    [],
    c_uint),

Modified: cfe/trunk/bindings/python/tests/cindex/test_cursor.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_cursor.py?rev=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_cursor.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_cursor.py Wed Jul 12 04:35:11 2017
@@ -255,6 +255,22 @@ def test_is_virtual_method():
     assert foo.is_virtual_method()
     assert not bar.is_virtual_method()
 
+def test_is_scoped_enum():
+    """Ensure Cursor.is_scoped_enum works."""
+    source = 'class X {}; enum RegularEnum {}; enum class ScopedEnum {};'
+    tu = get_tu(source, lang='cpp')
+
+    cls = get_cursor(tu, 'X')
+    regular_enum = get_cursor(tu, 'RegularEnum')
+    scoped_enum = get_cursor(tu, 'ScopedEnum')
+    assert cls is not None
+    assert regular_enum is not None
+    assert scoped_enum is not None
+
+    assert not cls.is_scoped_enum()
+    assert not regular_enum.is_scoped_enum()
+    assert scoped_enum.is_scoped_enum()
+
 def test_underlying_type():
     tu = get_tu('typedef int foo;')
     typedef = get_cursor(tu, 'foo')

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Jul 12 04:35:11 2017
@@ -4417,6 +4417,11 @@ CINDEX_LINKAGE unsigned clang_CXXMethod_
 CINDEX_LINKAGE unsigned clang_CXXMethod_isVirtual(CXCursor C);
 
 /**
+ * \brief Determine if an enum declaration refers to a scoped enum.
+ */
+CINDEX_LINKAGE unsigned clang_EnumDecl_isScoped(CXCursor C);
+
+/**
  * \brief Determine if a C++ member function or member function template is
  * declared 'const'.
  */

Modified: cfe/trunk/test/Index/print-type-declaration.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/print-type-declaration.cpp?rev=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/test/Index/print-type-declaration.cpp (original)
+++ cfe/trunk/test/Index/print-type-declaration.cpp Wed Jul 12 04:35:11 2017
@@ -7,6 +7,13 @@ int main()
   auto b = a;
 }
 
+enum RegularEnum {};
+
+enum class ScopedEnum {};
+
 // RUN: c-index-test -test-print-type-declaration -std=c++11 %s | FileCheck %s
 // CHECK: VarDecl=a:6:8 (Definition) [typedeclaration=Test] [typekind=Record]
 // CHECK: VarDecl=b:7:8 (Definition) [typedeclaration=Test] [typekind=Record]
+// CHECK: EnumDecl=RegularEnum:10:6 (Definition) [typedeclaration=RegularEnum] [typekind=Enum]
+// CHECK: EnumDecl=ScopedEnum:12:12 (Definition) (scoped) [typedeclaration=ScopedEnum] [typekind=Enum]
+

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=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed Jul 12 04:35:11 2017
@@ -804,6 +804,8 @@ static void PrintCursor(CXCursor Cursor,
       printf(" (const)");
     if (clang_CXXMethod_isPureVirtual(Cursor))
       printf(" (pure)");
+    if (clang_EnumDecl_isScoped(Cursor))
+      printf(" (scoped)");
     if (clang_Cursor_isVariadic(Cursor))
       printf(" (variadic)");
     if (clang_Cursor_isObjCOptional(Cursor))

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Wed Jul 12 04:35:11 2017
@@ -7807,6 +7807,15 @@ unsigned clang_CXXMethod_isVirtual(CXCur
   return (Method && Method->isVirtual()) ? 1 : 0;
 }
 
+unsigned clang_EnumDecl_isScoped(CXCursor C) {
+  if (!clang_isDeclaration(C.kind))
+    return 0;
+
+  const Decl *D = cxcursor::getCursorDecl(C);
+  auto *Enum = dyn_cast_or_null<EnumDecl>(D);
+  return (Enum && Enum->isScoped()) ? 1 : 0;
+}
+
 //===----------------------------------------------------------------------===//
 // Attribute introspection.
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=307771&r1=307770&r2=307771&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Wed Jul 12 04:35:11 2017
@@ -12,6 +12,7 @@ clang_CXXMethod_isConst
 clang_CXXMethod_isPureVirtual
 clang_CXXMethod_isStatic
 clang_CXXMethod_isVirtual
+clang_EnumDecl_isScoped
 clang_Cursor_getArgument
 clang_Cursor_getNumTemplateArguments
 clang_Cursor_getTemplateArgumentKind




More information about the cfe-commits mailing list