forked from SimonRichards/clang-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceLocation.cs
80 lines (65 loc) · 2.52 KB
/
SourceLocation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Text;
namespace ClangSharp {
public class SourceLocation : IComparable {
internal Interop.SourceLocation Native { get; private set; }
public File File { get; private set; }
public readonly int Line;
public readonly int Column;
public readonly int Offset;
internal unsafe SourceLocation(Interop.SourceLocation native) {
Native = native;
IntPtr file = IntPtr.Zero;
uint line, column, offset;
Interop.clang_getInstantiationLocation(Native, &file, out line, out column, out offset);
Line = (int)line;
Column = (int)column;
Offset = (int)offset;
File = new File(file);
}
public int OffsetAtStartOfLine {
get {
return Offset - Column + 1;
}
}
public static SourceLocation Null {
get { return new SourceLocation(Interop.clang_getNullLocation()); }
}
public bool Equals(SourceLocation other) {
return this == other;
}
public bool IsValid {
get {
return this != Null && !string.IsNullOrEmpty(File.Name);
}
}
public static bool operator ==(SourceLocation left, SourceLocation right) {
return left.File == right.File && left.Offset == right.Offset;
}
public static bool operator !=(SourceLocation left, SourceLocation right) {
return left.File != right.File || left.Offset != right.Offset;
}
public override bool Equals(object obj) {
return obj is SourceLocation && Equals((SourceLocation)obj);
}
public override int GetHashCode() {
return (File.ToString() + Offset).GetHashCode();
}
public override string ToString() {
return string.Format("{0}({1},{2})", File, Line, Column);
}
public int CompareTo(object obj) {
SourceLocation other = obj as SourceLocation;
if (other == null) {
throw new ArgumentException("Other object in comparison was of wrong type", "obj");
}
return Offset.CompareTo(other.Offset);
}
public static bool operator <(SourceLocation first, SourceLocation second) {
return first.Offset < second.Offset;
}
public static bool operator >(SourceLocation first, SourceLocation second) {
return first.Offset > second.Offset;
}
}
}