1 /**
2  * Copyright: Copyright (c) 2011 Jacob Carlborg. All rights reserved.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Jan 29, 2012
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module clang.SourceLocation;
8 
9 import clang.c.Index;
10 import clang.File;
11 import clang.Util;
12 
13 struct SourceLocation
14 {
15     mixin CX;
16 
17     struct Spelling
18     {
19         File file;
20         uint line;
21         uint column;
22         uint offset;
23     }
24 
25     @property static SourceLocation empty()
26     {
27         return SourceLocation(clang_getNullLocation());
28     }
29 
30     @property Spelling spelling() const
31     {
32         Spelling spell;
33 
34         clang_getSpellingLocation(
35             cx,
36             &spell.file.cx,
37             &spell.line,
38             &spell.column,
39             &spell.offset);
40 
41         return spell;
42     }
43 
44     @property Spelling expansion() const
45     {
46         Spelling spell;
47 
48         clang_getExpansionLocation(
49             cx,
50             &spell.file.cx,
51             &spell.line,
52             &spell.column,
53             &spell.offset);
54 
55         return spell;
56     }
57 
58     @property string path() const
59     {
60         return file.name;
61     }
62 
63     @property File file() const
64     {
65         File file;
66         clang_getExpansionLocation(cx, &file.cx, null, null, null);
67         return file;
68     }
69 
70     @property uint line() const
71     {
72         uint result;
73         clang_getExpansionLocation(cx, null, &result, null, null);
74         return result;
75     }
76 
77     @property uint column() const
78     {
79         uint result;
80         clang_getExpansionLocation(cx, null, null, &result, null);
81         return result;
82     }
83 
84     @property uint offset() const
85     {
86         uint result;
87         clang_getExpansionLocation(cx, null, null, null, &result);
88         return result;
89     }
90 
91     @property bool isFromMainFile() const
92     {
93         return clang_Location_isFromMainFile(cx) != 0;
94     }
95 
96     @property string toString() const
97     {
98         import std.format : format;
99         auto localSpelling = spelling;
100 
101         return format(
102             "SourceLocation(file = %s, line = %d, column = %d, offset = %d)",
103             localSpelling.file,
104             localSpelling.line,
105             localSpelling.column,
106             localSpelling.offset);
107     }
108 
109     @property string toColonSeparatedString() const
110     {
111         import std.format : format;
112         auto localSpelling = spelling;
113 
114         return format(
115             "%s:%d:%d",
116             localSpelling.file.name,
117             localSpelling.line,
118             localSpelling.column);
119     }
120 }