Hello,

This is my first ever patch to anything, so please let me know if I did
something wrong.

This patch adds support for the clang_Location_isFromMainFile and
clang_Location_isInSystemHeader functions in the libclang Python bindings.

The functions are added as member functions of the SourceLocation class.
Index: bindings/python/clang/cindex.py
===================================================================
--- bindings/python/clang/cindex.py	(revision 232334)
+++ bindings/python/clang/cindex.py	(working copy)
@@ -214,6 +214,17 @@
         """Get the file offset represented by this source location."""
         return self._get_instantiation()[3]
 
+    def is_in_main_file(self):
+        """
+        Determines whether this source location is in the main file of its
+        corresponding translation unit.
+        """
+        return conf.lib.clang_Location_isFromMainFile(self)
+
+    def is_in_system_header(self):
+        """Determines whether this source location is in a system header."""
+        return conf.lib.clang_Location_isInSystemHeader(self)
+
     def __eq__(self, other):
         return conf.lib.clang_equalLocations(self, other)
 
@@ -3381,6 +3392,14 @@
    _CXString,
    _CXString.from_result),
 
+  ("clang_Location_isFromMainFile",
+   [SourceLocation],
+   bool),
+
+  ("clang_Location_isInSystemHeader",
+   [SourceLocation],
+   bool),
+
   ("clang_Type_getAlignOf",
    [Type],
    c_longlong),
Index: bindings/python/tests/cindex/test_location.py
===================================================================
--- bindings/python/tests/cindex/test_location.py	(revision 232334)
+++ bindings/python/tests/cindex/test_location.py	(working copy)
@@ -1,10 +1,15 @@
+import os
+
 from clang.cindex import Cursor
 from clang.cindex import File
 from clang.cindex import SourceLocation
 from clang.cindex import SourceRange
+from clang.cindex import TranslationUnit
 from .util import get_cursor
 from .util import get_tu
 
+kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
+
 baseInput="int one;\nint two;\n"
 
 def assert_location(loc, line, column, offset):
@@ -93,3 +98,15 @@
     location3 = SourceLocation.from_position(tu, file, 1, 6)
     range3 = SourceRange.from_locations(location1, location3)
     assert range1 != range3
+
+def test_location_source_type():
+    path = os.path.join(kInputsDir, 'hello.cpp')
+    tu = TranslationUnit.from_source(path)
+
+    for cursor in tu.cursor.get_children():
+        if cursor.location.file.name == path:
+            assert cursor.location.is_in_main_file()
+            assert not cursor.location.is_in_system_header()
+        elif os.path.basename(cursor.location.file.name) == 'stdio.h':
+            assert not cursor.location.is_in_main_file()
+            assert cursor.location.is_in_system_header()
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to