RedX2501 retitled this revision from "Added functions to retrieve information 
about variable storage in libclang and its python bindings." to "Added 
functions to retrieve information about whether a vardecl is local in libclang 
and its python bindings.".
RedX2501 updated this revision to Diff 39163.
RedX2501 added a comment.

No longer export to functions but only one.
The original functionality can be achieved by using StorageClass coupled with 
the information if the variable is local.

Added python and libclang tests.


http://reviews.llvm.org/D10834

Files:
  bindings/python/clang/cindex.py
  bindings/python/tests/cindex/test_cursor.py
  include/clang-c/Index.h
  test/Index/islocalvardecl.cpp
  tools/c-index-test/c-index-test.c
  tools/libclang/CIndex.cpp
  tools/libclang/libclang.exports

Index: tools/libclang/libclang.exports
===================================================================
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -32,6 +32,7 @@
 clang_Cursor_isNull
 clang_Cursor_isObjCOptional
 clang_Cursor_isVariadic
+clang_Cursor_isLocalVarDecl
 clang_Cursor_getModule
 clang_Cursor_getStorageClass
 clang_File_isEqual
Index: tools/libclang/CIndex.cpp
===================================================================
--- tools/libclang/CIndex.cpp
+++ tools/libclang/CIndex.cpp
@@ -6741,6 +6741,19 @@
   return 0;
 }
 
+unsigned clang_Cursor_isLocalVarDecl(CXCursor C){
+  if (C.kind != CXCursor_VarDecl) {
+    return 0;
+  }
+
+  const Decl *D = getCursorDecl(C);
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+		return VD->isLocalVarDecl();
+	}
+
+	return 0;
+}
+
 CXSourceRange clang_Cursor_getCommentRange(CXCursor C) {
   if (!clang_isDeclaration(C.kind))
     return clang_getNullRange();
Index: tools/c-index-test/c-index-test.c
===================================================================
--- tools/c-index-test/c-index-test.c
+++ tools/c-index-test/c-index-test.c
@@ -1248,6 +1248,21 @@
 }
 
 /******************************************************************************/
+/* Local VarKind testing.                                                     */
+/******************************************************************************/
+
+static enum ChildVisitResult PrintIsLocalVarDecl(CXCursor C, CXClientData CD){
+
+  if (clang_getCursorKind(C) != CXCursor_VarDecl)
+    return CXChildVisit_Recurse;
+
+  PrintCursor(C, NULL);
+  printf(" IsLocalVarDecl=%d\n", clang_Cursor_isLocalVarDecl(C));
+
+  return CXChildVisit_Recurse;
+}
+
+/******************************************************************************/
 /* Typekind testing.                                                          */
 /******************************************************************************/
 
@@ -4065,6 +4080,7 @@
     "       c-index-test -test-inclusion-stack-tu <AST file>\n");
   fprintf(stderr,
     "       c-index-test -test-print-linkage-source {<args>}*\n"
+    "       c-index-test -test-print-is-local-var {<args>}*\n"
     "       c-index-test -test-print-type {<args>}*\n"
     "       c-index-test -test-print-type-size {<args>}*\n"
     "       c-index-test -test-print-bitwidth {<args>}*\n"
@@ -4152,6 +4168,9 @@
   else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
     return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
                                     NULL);
+	else if (argc > 2 && strcmp(argv[1], "-test-print-is-local-var") == 0)
+    return perform_test_load_source(argc - 2, argv + 2, "all", PrintIsLocalVarDecl,
+                                    NULL);
   else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
     return perform_test_load_source(argc - 2, argv + 2, "all",
                                     PrintType, 0);
Index: test/Index/islocalvardecl.cpp
===================================================================
--- /dev/null
+++ test/Index/islocalvardecl.cpp
@@ -0,0 +1,32 @@
+// RUN: c-index-test -test-print-local-var-kind %s | FileCheck %s
+
+extern "C" {
+  int var0;
+  static int var1;
+
+  void func(void){
+    static int var2;
+    int var3;
+  }
+};
+
+int var4;
+
+class Classy {
+  static int var5;
+
+  void member(){
+    int var6;
+    static int var7;
+  }
+};
+
+// CHECK: VarDecl=var0:3:5 (Definition) IsLocalVarDecl=0
+// CHECK: VarDecl=var1:4:12 (Definition) IsLocalVarDecl=0
+// CHECK: VarDecl=var2:7:14 (Definition) IsLocalVarDecl=1
+// CHECK: VarDecl=var3:8:7 (Definition) IsLocalVarDecl=1
+// CHECK: VarDecl=var4:13:5 (Definition) IsLocalVarDecl=0
+// CHECK: VarDecl=var5:17:12 IsLocalVarDecl=0
+// CHECK: VarDecl=var6:20:9 (Definition) IsLocalVarDecl=1
+// CHECK: VarDecl=var7:21:16 (Definition) IsLocalVarDecl=1
+
Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h
+++ include/clang-c/Index.h
@@ -3829,6 +3829,11 @@
 CINDEX_LINKAGE unsigned clang_Cursor_isVariadic(CXCursor C);
 
 /**
+ * \brief Returns non-zero if the Cursor refers to a local VarDecl.
+ */
+CINDEX_LINKAGE unsigned clang_Cursor_isLocalVarDecl(CXCursor C);
+
+/**
  * \brief Given a cursor that represents a declaration, return the associated
  * comment's source range.  The range may include multiple consecutive comments
  * with whitespace in between.
Index: bindings/python/tests/cindex/test_cursor.py
===================================================================
--- bindings/python/tests/cindex/test_cursor.py
+++ bindings/python/tests/cindex/test_cursor.py
@@ -279,6 +279,48 @@
     else:
         assert False, "Couldn't find annotation"
 
+local_var = """
+extern "C" {
+  int var0;
+  static int var1;
+
+  void func(void){
+    static int var2;
+    int var3;
+  }
+};
+
+int var4;
+
+class Classy {
+  static int var5;
+
+  void member(){
+    int var6;
+    static int var7;
+  }
+};
+"""
+
+def test_is_local_var():
+    tu = get_tu(local_var, lang="cpp")
+    var0 = get_cursor(tu, "var0")
+    var1 = get_cursor(tu, "var1")
+    var2 = get_cursor(tu, "var2")
+    var3 = get_cursor(tu, "var3")
+    var4 = get_cursor(tu, "var4")
+    var5 = get_cursor(tu, "var5")
+    var6 = get_cursor(tu, "var6")
+    var7 = get_cursor(tu, "var7")
+    assert not var0.is_local_var
+    assert not var1.is_local_var
+    assert var2.is_local_var
+    assert var3.is_local_var
+    assert not var4.is_local_var
+    assert not var5.is_local_var
+    assert var6.is_local_var
+    assert var7.is_local_var
+    
 def test_result_type():
     tu = get_tu('int foo();')
     foo = get_cursor(tu, 'foo')
@@ -370,3 +412,4 @@
     # [c-index-test handles this by running the source through clang, emitting
     #  an AST file and running libclang on that AST file]
     assert foo.mangled_name in ('_Z3fooii', '__Z3fooii', '?foo@@YAHHH')
+
Index: bindings/python/clang/cindex.py
===================================================================
--- bindings/python/clang/cindex.py
+++ bindings/python/clang/cindex.py
@@ -1430,6 +1430,13 @@
         return self._referenced
 
     @property
+    def is_local_var(self):
+        if not hasattr(self, '_is_local_var'):
+            self._is_local_var = conf.lib.clang_Cursor_isLocalVarDecl(self)
+
+        return self._is_local_var
+
+    @property
     def brief_comment(self):
         """Returns the brief comment text associated with that Cursor"""
         return conf.lib.clang_Cursor_getBriefCommentText(self)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to