[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-commits
Subject: Re: [cfe-commits] [PATCH 1/2] [clang.py] Add TranslationUnit.get_{file, source_location, source_rang
From: Gregory Szorc <gregory.szorc () gmail ! com>
Date: 2012-06-30 2:15:09
Message-ID: CAKQoGan-z2DXEdZ0cU5B2BNtUwK_L9L19s4yNGAfgQ5Of-MVPg () mail ! gmail ! com
[Download RAW message or body]
Updated patch.
On Fri, Jun 29, 2012 at 8:47 AM, Gregory Szorc <gregory.szorc@gmail.com> wrote:
> Having thought about this in my sleep, I may want to rescind this
> review request and refactor things a little.
>
> 1) I may change "range" and "source_range" names to "extent" since
> that is what is used elsewhere.
> 2) I may combine the bounds to obtain ranges/extents from two
> arguments to 2-tuples.
> 3) I may remove the "_source" from get_source_location and
> get_source_extent. I don't think that's any less clear.
>
> This will of course invalidate the token API patch that followed,
> albeit trivially.
>
> On Fri, Jun 29, 2012 at 12:13 AM, Gregory Szorc <gregory.szorc@gmail.com> wrote:
>> These are just convenience APIs to make obtaining File,
>> SourceLocation, and SourceRange instances easier.
>>
>> Old way:
>>
>> f = File.from_name(tu, 'foo.c')
>> location = SourceLocation.from_offset(tu, f, 10)
>>
>> New way:
>>
>> location = tu.get_source_location('foo.c', offset=10)
["0001-clang.py-Add-TranslationUnit.get_-file-source_locati.patch" (application/octet-stream)]
From 048caee0f2bd589758990cb76f4396fdd5101d4a Mon Sep 17 00:00:00 2001
From: Gregory Szorc <gregory.szorc@gmail.com>
Date: Thu, 28 Jun 2012 23:48:12 -0700
Subject: [PATCH 1/2] [clang.py] Add
TranslationUnit.get_{file,source_location,source_range}
---
bindings/python/clang/cindex.py | 49 ++++++++++++++++
.../python/tests/cindex/test_translation_unit.py | 66 ++++++++++++++++++++++
2 files changed, 115 insertions(+)
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index 75ab489..41835d8 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -1890,16 +1890,65 @@ class TranslationUnit(ClangObject):
# Automatically adapt CIndex/ctype pointers to python objects
includes = []
lib.clang_getInclusions(self,
callbacks['translation_unit_includes'](visitor), includes)
return iter(includes)
+ def get_file(self, filename):
+ """Obtain a File from this translation unit."""
+
+ return File.from_name(self, filename)
+
+ def get_location(self, filename, offset=None, line=None):
+ """Obtain a SourceLocation for a file in this translation unit.
+
+ The SourceLocation can be specified by either definining the file
+ offset or by a line-column 2-tuple. If both are defined, behavior is
+ undefined.
+ """
+ f = self.get_file(filename)
+
+ if offset is not None:
+ return SourceLocation.from_offset(self, f, offset)
+ else:
+ return SourceLocation.from_position(self, f, line[0], line[1])
+
+ def get_extent(self, filename, locations=None, offsets=None, lines=None):
+ """Obtain a SourceRange from this translation unit.
+
+ The bounds of the SourceRange must ultimately be defined by a start and
+ end SourceLocation. You can pass two instances as a 2-tuple via
+ locations. You can also pass two integer file offsets via offsets. Or,
+ you can pass two line-column 2-tuples via lines.
+
+ If multiple types of range bounds are defined, behavior is undefined.
+ """
+ f = self.get_file(filename)
+
+ start_location = None
+ end_location = None
+
+ if locations is not None:
+ start_location, end_location = locations
+
+ if offsets is not None:
+ start_location = SourceLocation.from_offset(self, f, offsets[0])
+ end_location = SourceLocation.from_offset(self, f, offsets[1])
+
+ if lines is not None:
+ start_location = SourceLocation.from_position(self, f, lines[0][0],
+ lines[0][1])
+ end_location = SourceLocation.from_position(self, f, lines[1][0],
+ lines[1][1])
+
+ return SourceRange.from_locations(start_location, end_location)
+
@property
def diagnostics(self):
"""
Return an iterable (and indexable) object containing the diagnostics.
"""
class DiagIterator:
def __init__(self, tu):
self.tu = tu
diff --git a/bindings/python/tests/cindex/test_translation_unit.py \
b/bindings/python/tests/cindex/test_translation_unit.py index 982a608..f45f9ca 100644
--- a/bindings/python/tests/cindex/test_translation_unit.py
+++ b/bindings/python/tests/cindex/test_translation_unit.py
@@ -1,11 +1,14 @@
from clang.cindex import CursorKind
from clang.cindex import Cursor
+from clang.cindex import File
from clang.cindex import Index
+from clang.cindex import SourceLocation
+from clang.cindex import SourceRange
from clang.cindex import TranslationUnitSaveError
from clang.cindex import TranslationUnit
from .util import get_cursor
from .util import get_tu
import os
kInputsDir = os.path.join(os.path.dirname(__file__), 'INPUTS')
@@ -146,8 +149,71 @@ def test_load():
os.unlink(path)
def test_index_parse():
path = os.path.join(kInputsDir, 'hello.cpp')
index = Index.create()
tu = index.parse(path)
assert isinstance(tu, TranslationUnit)
+
+def test_get_file():
+ """Ensure tu.get_file() works appropriately."""
+
+ tu = get_tu('int foo();')
+
+ f = tu.get_file('t.c')
+ assert isinstance(f, File)
+ assert f.name == 't.c'
+
+ try:
+ f = tu.get_file('foobar.cpp')
+ except:
+ pass
+ else:
+ assert False
+
+def test_get_source_location():
+ """Ensure tu.get_source_location() works."""
+
+ tu = get_tu('int foo();')
+
+ location = tu.get_location('t.c', offset=2)
+ assert isinstance(location, SourceLocation)
+ assert location.offset == 2
+ assert location.file.name == 't.c'
+
+ location = tu.get_location('t.c', line=(1, 3))
+ assert isinstance(location, SourceLocation)
+ assert location.line == 1
+ assert location.column == 3
+ assert location.file.name == 't.c'
+
+def test_get_source_range():
+ """Ensure tu.get_source_range() works."""
+
+ tu = get_tu('int foo();')
+
+ r = tu.get_extent('t.c', offsets=(1,4))
+ assert isinstance(r, SourceRange)
+ assert r.start.offset == 1
+ assert r.end.offset == 4
+ assert r.start.file.name == 't.c'
+ assert r.end.file.name == 't.c'
+
+ r = tu.get_extent('t.c', lines=((1,2), (1,3)))
+ assert isinstance(r, SourceRange)
+ assert r.start.line == 1
+ assert r.start.column == 2
+ assert r.end.line == 1
+ assert r.end.column == 3
+ assert r.start.file.name == 't.c'
+ assert r.end.file.name == 't.c'
+
+ start = tu.get_location('t.c', offset=0)
+ end = tu.get_location('t.c', offset=5)
+
+ r = tu.get_extent('t.c', locations=(start, end))
+ assert isinstance(r, SourceRange)
+ assert r.start.offset == 0
+ assert r.end.offset == 5
+ assert r.start.file.name == 't.c'
+ assert r.end.file.name == 't.c'
--
1.7.11.1
_______________________________________________
cfe-commits mailing list
cfe-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic