1 /** 2 * Copyright: Copyright (c) 2016 Wojciech Szęszoł. All rights reserved. 3 * Authors: Wojciech Szęszoł 4 * Version: Initial created: Feb 14, 2016 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) 6 */ 7 module clang.SourceRange; 8 9 import std.conv; 10 import clang.c.Index; 11 import clang.SourceLocation; 12 import clang.Util; 13 14 struct SourceRange 15 { 16 mixin CX; 17 18 this(CXSourceRange cx) 19 { 20 this.cx = cx; 21 } 22 23 @property static SourceRange empty() 24 { 25 return SourceRange(clang_getNullRange()); 26 } 27 28 @property bool isEmpty() 29 { 30 return clang_Range_isNull(cx) != 0; 31 } 32 33 this(SourceLocation start, SourceLocation end) 34 { 35 cx = clang_getRange(start.cx, end.cx); 36 } 37 38 @property SourceLocation start() const 39 { 40 return SourceLocation(clang_getRangeStart(cx)); 41 } 42 43 @property SourceLocation end() const 44 { 45 return SourceLocation(clang_getRangeEnd(cx)); 46 } 47 48 @property bool isMultiline() const 49 { 50 return start.line != end.line; 51 } 52 53 @property string path() const 54 { 55 return start.path; 56 } 57 58 @property string toString() const 59 { 60 import std.format: format; 61 return format("SourceRange(start = %s, end = %s)", start, end); 62 } 63 } 64 65 bool intersects(in SourceRange a, in SourceRange b) 66 { 67 return a.path == b.path && 68 (a.start.offset <= b.start.offset && b.start.offset < a.end.offset) || 69 (a.start.offset < b.end.offset && b.end.offset <= a.end.offset); 70 } 71 72 bool contains(in SourceRange a, in SourceRange b) 73 { 74 return a.path == b.path && 75 a.start.offset <= b.start.offset && b.end.offset <= a.end.offset; 76 }