[clang] c7161e7 - [python] Expose clang_Location_isInSystemHeader

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 4 06:21:35 PDT 2023


Author: Artur Ryt
Date: 2023-04-04T09:21:04-04:00
New Revision: c7161e73ca0afdf9cc016ce4e9f1e23a6a140b51

URL: https://github.com/llvm/llvm-project/commit/c7161e73ca0afdf9cc016ce4e9f1e23a6a140b51
DIFF: https://github.com/llvm/llvm-project/commit/c7161e73ca0afdf9cc016ce4e9f1e23a6a140b51.diff

LOG: [python] Expose clang_Location_isInSystemHeader

Add is_in_system_header property for Location class.

Corresponding unit test was also added.

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

Added: 
    

Modified: 
    clang/bindings/python/clang/cindex.py
    clang/bindings/python/tests/cindex/test_location.py
    clang/docs/ReleaseNotes.rst

Removed: 
    


################################################################################
diff  --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 5d13b7bff1498..6d33650a71671 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -286,6 +286,11 @@ def offset(self):
         """Get the file offset represented by this source location."""
         return self._get_instantiation()[3]
 
+    @property
+    def is_in_system_header(self):
+        """Returns true if the given 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)
 
@@ -4131,6 +4136,10 @@ def cursor(self):
    [Cursor],
    c_longlong),
 
+  ("clang_Location_isInSystemHeader",
+   [SourceLocation],
+   bool),
+
   ("clang_Type_getAlignOf",
    [Type],
    c_longlong),

diff  --git a/clang/bindings/python/tests/cindex/test_location.py b/clang/bindings/python/tests/cindex/test_location.py
index fbe9770d7ebc5..0d2c69db883cc 100644
--- a/clang/bindings/python/tests/cindex/test_location.py
+++ b/clang/bindings/python/tests/cindex/test_location.py
@@ -7,6 +7,7 @@
 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
 
@@ -103,3 +104,17 @@ def test_extent(self):
         location3 = SourceLocation.from_position(tu, file, 1, 6)
         range3 = SourceRange.from_locations(location1, location3)
         self.assertNotEqual(range1, range3)
+
+    def test_is_system_location(self):
+        header = os.path.normpath('./fake/fake.h')
+        tu = TranslationUnit.from_source('fake.c', [f'-isystem{os.path.dirname(header)}'], unsaved_files = [
+                ('fake.c', """
+#include <fake.h>
+int one;
+"""),
+                (header, "int two();")
+                ])
+        one = get_cursor(tu, 'one')
+        two = get_cursor(tu, 'two')
+        self.assertFalse(one.location.is_in_system_header)
+        self.assertTrue(two.location.is_in_system_header)

diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 100be1b5e893c..e39c8606ffd38 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -451,6 +451,12 @@ Static Analyzer
 Sanitizers
 ----------
 
+Python Binding Changes
+----------------------
+The following methods have been added:
+
+- ``clang_Location_isInSystemHeader`` exposed via the ``is_in_system_header``
+  property of the `Location` class.
 
 Additional Information
 ======================


        


More information about the cfe-commits mailing list